-
-
Notifications
You must be signed in to change notification settings - Fork 437
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
Replaces full class name with self #962
Conversation
@@ -134,7 +134,7 @@ public function getRate($toCurrency) | |||
{ | |||
if (is_string($toCurrency)) { | |||
$code = $toCurrency; | |||
} elseif ($toCurrency instanceof Mage_Directory_Model_Currency) { | |||
} elseif ($toCurrency instanceof self) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this still work if you override Mage_Directory_Model_Currency class with custom class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, overrides still work ...
<?php
class A
{
public static function test($class)
{
if ($class instanceof self) {
echo 'yes';
}
}
}
class B extends A
{}
A::test(new B());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem will occur when people will override the class and copy some methods to change them, ending up with code:
<?php
class A
{
public static function test($class)
{
if ($class instanceof self) {
echo 'yes';
}
}
}
class B extends A
{
public static function test($class)
{
if ($class instanceof self) {
echo 'yes';
}
//some additional code modification
}
}
B::test(new A());
This will yeld wrong results.
So I'm against changing class name to self in "instanceof".
The logic of instanceof is to check whether sth implements a certain interface. Making it dynamic will cause hard to debug issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this break current code? Guess not.
It is just a problem if someone would copy&paste mehtods, w/o thinking about it .... For existing code it should not matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this change since it does not break anything and it is up to coders who copy and paste to do so correctly, I think that has always been the case. However, I see it as a minor point. @sreichel what is the case for doing this as it relates to the end goal to use PHPCS and PHPStan? If we don't include this change will it always be throwing up warnings/errors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@colinmollenhour this is not related to phpCS or phpStan ... it will not throw errors. Just want to have clean code ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, seems like a minor subjective issue so I will approve as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, it's just making people life harder without much benefit.
so +1 for the change in general -1 for the "instanceof" change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, it's just making people life harder without much benefit.
Mhh ... at the other hand it enforces coders to review their copy&paste-code a bit more. I'd leave this open for the next days and more opinios about that. OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree with "instanceof" change
As said .... it does not affect current copied code ...
|
Iam not Approving or Disapproving here, but I want to note two things.
It is not enforcing them to, it is just punishing them for not doing it more
Clean Code has a very wide spectrum from "it looks subjectively better this way" to "there is a well described and documented case of why this format/pattern leads to less bugs and easier understanding" So going through it now myself. using self for the method and constant access is increasing the extensibility, so definitive worth it. For the Its probably safe to merge, but could throw people off, who are trying to extend this parts in the future. (and we know, Magento Devs will make clear, if the framework is causing issues) I would like to encourage you all, to feel more likely to split PullRequests up, when only parts lead to discussion, which are easy to do independently. Also a few Informations to the context of the changes and links to explainations why certain patterns and styles are useful to apply could help avoid discussions and search for proof of correctness. |
Why is this in draft status? |
See @Flyingmana comment. |
I feel bad for not knowing but is this covered by any PSR or Magento Coding Standard? |
Guess not. I'll split this PR. |
@sreichel Please fix conflicts. Thanks! |
No description provided.