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

Replaces full class name with self #962

Closed
wants to merge 3 commits into from
Closed

Replaces full class name with self #962

wants to merge 3 commits into from

Conversation

sreichel
Copy link
Contributor

No description provided.

@sreichel sreichel added the Cleanup: Code style Related to simple CS fixes. label May 11, 2020
@@ -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) {
Copy link
Contributor

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?

Copy link
Contributor Author

@sreichel sreichel May 11, 2020

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());

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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 ...

Copy link
Member

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.

Copy link
Contributor

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

Copy link
Contributor Author

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?

@sreichel sreichel requested a review from tmotyl May 12, 2020 04:26
Copy link
Contributor

@tmotyl tmotyl left a 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

@sreichel sreichel added hold and removed hold labels May 12, 2020
@sreichel
Copy link
Contributor Author

I don't agree with "instanceof" change

As said .... it does not affect current copied code ...

it does not break anything and it is up to coders who copy and paste to do so correctly,

@sreichel sreichel requested a review from tmotyl May 19, 2020 05:03
kiatng
kiatng previously approved these changes May 19, 2020
@Flyingmana
Copy link
Contributor

Flyingmana commented May 19, 2020

Iam not Approving or Disapproving here, but I want to note two things.

Mhh ... at the other hand it enforces coders to review their copy&paste-code a bit more.

It is not enforcing them to, it is just punishing them for not doing it more

this is not related to phpCS or phpStan ... it will not throw errors. Just want to have clean code ...
It is not clear under which aspect of Clean Code this falls.

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 instance of it is in this cases used like an interface, to make sure a certain method exists.
When extending, this behavior should usually be the same.
So what benefit does the `self`` bring. It makes renaming the Class easier. It also makes it easier to copy the code to a new Class (not an extended class), both use cases which I dont see us have.
Even with searching I did not find any other beneficial use case.

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)
But if this happens, its easy to revert this part, as there are no big benefits getting lost by reverting.

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.
We do this Reviews because its hard for a single person to know all the side effects a change can have, and many eyes usually help finding them.

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.

@sreichel sreichel marked this pull request as draft May 19, 2020 08:16
@colinmollenhour
Copy link
Member

Why is this in draft status?

@sreichel sreichel marked this pull request as ready for review June 5, 2020 21:54
@sreichel sreichel dismissed stale reviews from kiatng and colinmollenhour via 7814ea5 June 5, 2020 21:56
@sreichel
Copy link
Contributor Author

sreichel commented Jun 5, 2020

Why is this in draft status?

See @Flyingmana comment.

@colinmollenhour
Copy link
Member

I feel bad for not knowing but is this covered by any PSR or Magento Coding Standard?

@sreichel
Copy link
Contributor Author

Guess not. I'll split this PR.

@colinmollenhour
Copy link
Member

@sreichel Please fix conflicts. Thanks!

@sreichel sreichel assigned sreichel and unassigned sreichel Jul 7, 2020
@github-actions github-actions bot added Component: Api2 Relates to Mage_Api2 Component: Captcha Relates to Mage_Captcha Component: Core Relates to Mage_Core Component: Directory Relates to Mage_Directory Component: ImportExport Relates to Mage_ImportExport labels Jul 24, 2020
@github-actions github-actions bot added Component: Oauth Relates to Mage_Oauth Component: PayPal Relates to Mage_Paypal Component: PaypalUk Relates to Mage_PaypalUk Component: Sales Relates to Mage_Sales Component: SalesRule Relates to Mage_SalesRule Component: Usa Relates to Mage_Usa Component: XmlConnect labels Jul 24, 2020
@sreichel sreichel closed this Mar 17, 2021
@sreichel sreichel mentioned this pull request Nov 20, 2022
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cleanup: Code style Related to simple CS fixes. Component: Api2 Relates to Mage_Api2 Component: Captcha Relates to Mage_Captcha Component: Core Relates to Mage_Core Component: Directory Relates to Mage_Directory Component: ImportExport Relates to Mage_ImportExport Component: Oauth Relates to Mage_Oauth Component: PayPal Relates to Mage_Paypal Component: PaypalUk Relates to Mage_PaypalUk Component: Sales Relates to Mage_Sales Component: SalesRule Relates to Mage_SalesRule Component: Usa Relates to Mage_Usa
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants