Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed PHPStan error in Mage_HTTP_Client_Socket and Mage_HTTP_Client_Curl #3205

Merged
merged 5 commits into from
May 15, 2023
Merged

Fixed PHPStan error in Mage_HTTP_Client_Socket and Mage_HTTP_Client_Curl #3205

merged 5 commits into from
May 15, 2023

Conversation

fballiano
Copy link
Contributor

I rewrote the list($key, $val) = explode("=", $values[0]);

But it seems to me that the whole Mage_HTTP* is never used, should we at least deprecate it? or remove it in next?

@github-actions github-actions bot added Component: lib/Mage Relates to lib/Mage Component: lib/* Relates to lib/* phpstan labels Apr 21, 2023
@justinbeaty
Copy link
Contributor

I guess PHPStan is a bit wrong here, because $val very well could have been null:

list($key, $val) = explode('=', 'foo');

var_dump($key); // string(3) "foo"'
var_dump($val); // NULL

I'd be curious if it throws the error if you used if ($val === null). Alternatively, you could use if (!array_key_exists(1, $keyValueArray)) (since index 0 will always be set). Or even just if (count($keyValueArray) >= 2).


It seems like some people might be using this library, see: #913. However, I support the removal of these classes (move it to an external repo, I suppose.) We should encourage people to use the power of Composer here, and just install something like Guzzle or one of the many other helper libraries. These built-in libraries like this are so old-school.

@fballiano
Copy link
Contributor Author

@justinbeaty I've removed the first unnecessary check. I wouldn't care too much about phpstan sincerely because it also is not perfect... anyway since nowadays if you don't do phpstand you're not cool... it bothers me to have a million errors.

what if we deprecate all these classes in lib/Mage, copy them to a separate repo and remove them from next?

@fballiano fballiano changed the title Fixed PHPStan error in Mage_HTTP_Client_Socket Fixed PHPStan error in Mage_HTTP_Client_Socket and Mage_HTTP_Client_Curl Apr 21, 2023
@justinbeaty
Copy link
Contributor

what if we deprecate all these classes in lib/Mage, copy them to a separate repo and remove them from next?

I think it's okay to deprecate / remove. It's an easy fix for someone to add back if they used it.

There's probably a lot more in that lib/Mage folder we can remove...

@fballiano
Copy link
Contributor Author

I've removed the whole lib/Mage and see no error browsing around...

@justinbeaty
Copy link
Contributor

justinbeaty commented Apr 21, 2023

I think Mage_Exception might be the only used class from there.

These are the classes I see defined:

$ grep -Roh 'class Mage[_A-Za-z]*' lib/Mage | cut -d' ' -f 2
Mage_System_Ftp
Mage_System_Dirs
Mage_System_Args
Mage_HTTP_Client
Mage_HTTP_Client_Curl
Mage_HTTP_Client_Socket
Mage_Exception
Mage_Archive
Mage_Archive_Tar
Mage_Archive_Gz
Mage_Archive_Bz
Mage_Archive_Abstract
Mage_Archive_Helper_File
Mage_Archive_Helper_File_Gz
Mage_Archive_Helper_File_Bz
Mage_DB_Exception
Mage_DB_Mysqli
Mage_Xml_Parser
Mage_Xml_Generator
Mage_Cache_Backend_File
Mage_Cache_Backend_Redis

@fballiano
Copy link
Contributor Author

you're considering lib/Mage too I think, cause I can see only these occurrences:
Screenshot 2023-04-21 alle 21 08 24

which should be converted to Mage_Core_Exception I think

@fballiano
Copy link
Contributor Author

ps: same think could be for lib/Magento... more or less..

@justinbeaty
Copy link
Contributor

justinbeaty commented Apr 21, 2023

you're considering lib/Mage too I think, cause I can see only these occurrences:

I meant that Mage_Exception was the only class defined in lib/Mage that was used outside of lib/Mage. We're saying the same thing.

To figure this out, I grepped for each of those classes in app/ and found nothing. I also grepped for mage/archive etc, but I don't think they're loadable via getModel anyway.

I think we could rewrite Mage_Exception -> Mage_Core_Exceptoin too. I don't know why there's two, probably just legacy crap. If needed, we can even do this somewhere:

if (!class_exists('Mage_Exception')) {
  class Mage_Exception extends Exception {}
}

@fballiano
Copy link
Contributor Author

legacy crap 😂

@fballiano
Copy link
Contributor Author

I'd do all of these things in separate PRs anyway

justinbeaty
justinbeaty previously approved these changes Apr 21, 2023
@justinbeaty
Copy link
Contributor

I'd do all of these things in separate PRs anyway

Yeah, so then this PR is fine to me. approved.

@sreichel
Copy link
Contributor

sreichel commented Apr 21, 2023

I guess PHPStan is a bit wrong here, because $val very well could have been null:

list($key, $val) = explode('=', 'foo');

var_dump($key); // string(3) "foo"'
var_dump($val); // NULL

I guess phpstan "thinks correct" here, but should give a hint about error in list().

The given example throws a notice on php7 (Notice: Undefined offset: 1 in list(....) = explode(...)) and a warning for php8 (Warning: Undefined array key 1)

Suggestion:

list($key, $val) = array_pad(explode('=', $values[0]), 2, null);

https://phpstan.org/r/6cef421c-5e0e-411e-9782-3a1f17b8aa6d

@fballiano
Copy link
Contributor Author

i've refactored this one

@fballiano fballiano requested review from justinbeaty and kiatng and removed request for justinbeaty May 10, 2023 15:28
@sreichel
Copy link
Contributor

LGTM, but do we need !strlen($key)?

@fballiano
Copy link
Contributor Author

mmm I thought it would be better to check the key too and not only the value, in case after the trim it becomes empty, dunno.

@fballiano fballiano merged commit f12eb44 into OpenMage:main May 15, 2023
@fballiano fballiano deleted the phpstansocket branch May 15, 2023 07:54
@sreichel
Copy link
Contributor

Port to v19?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: lib/Mage Relates to lib/Mage Component: lib/* Relates to lib/* phpstan
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants