Skip to content

[CodingStyle] Add new rector to replace hardcoded class name reference in string with class keyword reference#3613

Merged
TomasVotruba merged 1 commit intorectorphp:masterfrom
dobryy:replace-in-string-classes-rule
Jul 1, 2020
Merged

[CodingStyle] Add new rector to replace hardcoded class name reference in string with class keyword reference#3613
TomasVotruba merged 1 commit intorectorphp:masterfrom
dobryy:replace-in-string-classes-rule

Conversation

@dobryy
Copy link
Contributor

@dobryy dobryy commented Jun 29, 2020

It seems like SplitStringClassConstantToClassConstFetchRector might be replaced with this new rector.

Closes #3389

Comment on lines +68 to +70
$concat = new Concat($classConstFetch, new String_('::' . $after));
if (! empty($before)) {
$concat = new Concat(new String_($before), $classConstFetch);
$concat = new Concat($concat, new String_('::' . $after));
}
Copy link
Contributor Author

@dobryy dobryy Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba, I wanted to use something like this

+        $concat = new Concat($classConstFetch, new String_('::' . $after));
+        if (! empty($before)) {
+            $concat = new Concat(new String_($before), $concat);
+        }
-        $concat = new Concat($classConstFetch, new String_('::' . $after));
-        if (! empty($before)) {
-            $concat = new Concat(new String_($before), $classConstFetch);
-            $concat = new Concat($concat, new String_('::' . $after));
-        }

but for some reason it generates unexpected (to me :)) string

-'<?php if (' . (SomeClass::class . '::createCache($netteCacheStorage, %var,  $this->global->cacheStack, %node.array?)) { ?>')
+'<?php if (' . SomeClass::class . '::createCache($netteCacheStorage, %var,  $this->global->cacheStack, %node.array?)) { ?>'

Copy link
Member

@TomasVotruba TomasVotruba Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write it again here in a diff what you want and what's changed? It would be easier for me to read and understand.

-$value . 'String'
+$value . 'StringCorrect'

The shorter, the better (withotu all the long classssss names)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment was updated with diffs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still can't see the problem (too much nette irelevant code).
If it's extra ( . ) than it's irelevant, as concat basically joins it all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maximum simplification:

-'<?php if (' . (SomeClass::class . '::method()) { ?>')
+'<?php if (' . SomeClass::class . '::method()) { ?>'

Doesn't look like it just joins...

Copy link
Member

@TomasVotruba TomasVotruba Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does reordering of Concat node helps? I think they're working the other way around.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I concat in "proper" order everything works well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically... this code works well but doesn't look that optimized:

        $concat = new Concat($classConstFetch, new String_('::' . $after));
        if (! empty($before)) {
            $concat = new Concat(new String_($before), $classConstFetch);
            $concat = new Concat($concat, new String_('::' . $after));
        }

This code looks more optimized and from the first view should work fine but it doesn't

        $concat = new Concat($classConstFetch, new String_('::' . $after));
        if (! empty($before)) {
            $concat = new Concat(new String_($before), $concat);
        }

I'm just wondering why it doesn't and I don't mind to leave it as it is now :)

@dobryy dobryy marked this pull request as ready for review June 29, 2020 19:27
$classConstFetch = new ClassConstFetch(new FullyQualified(ltrim($possibleClass, '\\')), 'class');

$concat = new Concat($classConstFetch, new String_('::' . $after));
if (! empty($before)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what $before is

Copy link
Contributor Author

@dobryy dobryy Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$before is everything before the class name. See the regex https://regex101.com/r/Vv41Qr/1
In some cases, it might be empty and I don't think we would like to generate empty string before the class name.

-'' . ClassName . '::method()'
+ClassName . '::method()'

Copy link
Member

@TomasVotruba TomasVotruba Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Please add one test fixture for that too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready

@TomasVotruba TomasVotruba self-requested a review June 29, 2020 19:45
Copy link
Member

@TomasVotruba TomasVotruba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added first review. Technically it looks good 👍

@TomasVotruba TomasVotruba self-requested a review June 30, 2020 18:32
@TomasVotruba
Copy link
Member

Looks good! 👍

Please rebase on master, squash to 1 commit and it's ready to merge

@TomasVotruba
Copy link
Member

Thanks!

@TomasVotruba TomasVotruba merged commit 6f0c284 into rectorphp:master Jul 1, 2020
@TomasVotruba
Copy link
Member

TomasVotruba commented Jul 1, 2020

I just realized we should handle also this case:

-$value = 'App\SomeClass::someMethod() && App\SomeClass::someMethod()';
+$value = \App\SomeClass::class . 'someMethod() && ' . \App\SomeClass::class . 'someMethod()';

Could you cover it too in new PR?

@dobryy
Copy link
Contributor Author

dobryy commented Jul 3, 2020

I just realized we should handle also this case:

-$value = 'App\SomeClass::someMethod() && App\SomeClass::someMethod()';
+$value = \App\SomeClass::class . 'someMethod() && ' . \App\SomeClass::class . 'someMethod()';

Could you cover it too in new PR?

@TomasVotruba, ready - #3648

@TomasVotruba TomasVotruba changed the title Add new rector to replace hardcoded class name reference in string with class keyword reference [CodingStyle] Add new rector to replace hardcoded class name reference in string with class keyword reference Jul 4, 2020
TomasVotruba added a commit that referenced this pull request Apr 17, 2023
rectorphp/rector-src@fbb472f Rename PARALLEL_TIMEOUT_IN_SECONDS to PARALLEL_JOB_TIMEOUT_IN_SECONDS (#3613)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CodeQuality] Add rule to replace in-string classes

2 participants