[CodingStyle] Add new rector to replace hardcoded class name reference in string with class keyword reference#3613
[CodingStyle] Add new rector to replace hardcoded class name reference in string with class keyword reference#3613TomasVotruba merged 1 commit intorectorphp:masterfrom dobryy:replace-in-string-classes-rule
class keyword reference#3613Conversation
| $concat = new Concat($classConstFetch, new String_('::' . $after)); | ||
| if (! empty($before)) { | ||
| $concat = new Concat(new String_($before), $classConstFetch); | ||
| $concat = new Concat($concat, new String_('::' . $after)); | ||
| } |
There was a problem hiding this comment.
@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?)) { ?>'There was a problem hiding this comment.
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)
There was a problem hiding this comment.
The comment was updated with diffs
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Maximum simplification:
-'<?php if (' . (SomeClass::class . '::method()) { ?>')
+'<?php if (' . SomeClass::class . '::method()) { ?>'Doesn't look like it just joins...
There was a problem hiding this comment.
Does reordering of Concat node helps? I think they're working the other way around.
There was a problem hiding this comment.
If I concat in "proper" order everything works well.
There was a problem hiding this comment.
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 :)
rules/coding-style/src/Rector/String_/UseClassKeywordForClassNameResolutionRector.php
Show resolved
Hide resolved
rules/coding-style/src/Rector/String_/UseClassKeywordForClassNameResolutionRector.php
Show resolved
Hide resolved
rules/coding-style/src/Rector/String_/UseClassKeywordForClassNameResolutionRector.php
Show resolved
Hide resolved
| $classConstFetch = new ClassConstFetch(new FullyQualified(ltrim($possibleClass, '\\')), 'class'); | ||
|
|
||
| $concat = new Concat($classConstFetch, new String_('::' . $after)); | ||
| if (! empty($before)) { |
There was a problem hiding this comment.
$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()'There was a problem hiding this comment.
I see. Please add one test fixture for that too.
...yle/tests/Rector/String_/UseClassKeywordForClassNameResolutionRector/Fixture/fixture.php.inc
Outdated
Show resolved
Hide resolved
...g_/UseClassKeywordForClassNameResolutionRector/UseClassKeywordForClassNameResolutionTest.php
Show resolved
Hide resolved
TomasVotruba
left a comment
There was a problem hiding this comment.
I've added first review. Technically it looks good 👍
...yle/tests/Rector/String_/UseClassKeywordForClassNameResolutionRector/Fixture/fixture.php.inc
Show resolved
Hide resolved
|
Looks good! 👍 Please rebase on |
…th `class` keyword reference
|
Thanks! |
|
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 |
class keyword referenceclass keyword reference
rectorphp/rector-src@fbb472f Rename PARALLEL_TIMEOUT_IN_SECONDS to PARALLEL_JOB_TIMEOUT_IN_SECONDS (#3613)
It seems like
SplitStringClassConstantToClassConstFetchRectormight be replaced with this new rector.Closes #3389