Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,52 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (substr_count($node->value, '::') !== 1) {
return null;
}
// a possible static call reference
// @see https://regex101.com/r/Vv41Qr/1/
[$before, $possibleClass, $after] = Strings::split($node->value, '#([\\\\a-zA-Z0-9_\\x80-\\xff]*)::#');
$classNames = $this->getExistentClasses($node);

if (! $possibleClass || ! class_exists($possibleClass)) {
return null;
if ($classNames === []) {
return $node;
}

$classConstFetch = new ClassConstFetch(new FullyQualified(ltrim($possibleClass, '\\')), 'class');
$parts = $this->getParts($node, $classNames);

foreach ($parts as $part) {
if (class_exists($part)) {
$returnNode = new ClassConstFetch(new FullyQualified(ltrim($part, '\\')), 'class');
} else {
$returnNode = new String_($part);
}

$concat = ! isset($concat) ? $returnNode : new Concat($concat, $returnNode);
Copy link
Member

Choose a reason for hiding this comment

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

This looks very crazy 😄 What is going on here?
It's some node stacking in a loop?

Better collect concat nodes in an array, e.g. to $contacts

$concats[] = $concat;

And resolve later in in go, e.g.

if ($concats === []) {
    return ...;
}

return ...;

It's much easier to debug and test in case of troubles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This solution helped me to keep myself away from becoming crazy :D Believe me or not but is a million percents better than a couple of the solution I had before this one :D

Maybe it is not clear but this solution is able to process the random amount of class name occurrences in the string (not only one or two) that is why concatenation in the loop is required.

When it comes to the condition it helps to avoid the first empty Node. If I won't use it I'll have to create the first Node with an empty value outside of the loop for first concatenation which will then produce empty node which will in final result look like

'' . \App\ClassName()::class . 'staticCall()'

While it is not a problem to create the array of nodes with the current solution... I can't imagine what I should implement under ... in your code to cover the cases in the test fixtures.

Copy link
Member

Choose a reason for hiding this comment

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

I see, I think what you mean. Nodes can break human brain sometimes, I've been there :D.
Don't worry about it 👍 , I'll check if there are is some space after we merge this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure how much you care about perfomance but to make this loop a bit nicer I can also

+            $returnNode = new String_($part);
+            if (class_exists($part)) {
+                $returnNode = new ClassConstFetch(new FullyQualified(ltrim($part, '\\')), 'class');
+            }
-            if (class_exists($part)) {
-                $returnNode = new ClassConstFetch(new FullyQualified(ltrim($part, '\\')), 'class');
-            } else {
-                $returnNode = new String_($part);
-            }

Copy link
Member

Choose a reason for hiding this comment

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

Unless a memory/performance leak, it's not an issue as this is CLI app, not a response/request one.

Overriding variable like this is confusing. Personally I'd extract it to own method too. E.g. createReturnNode() with early returns.

But current state is also ok

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, I think what you mean. Nodes can break human brain sometimes, I've been there :D.
Don't worry about it , I'll check if there are is some space after we merge this.

Not sure what do you mean by that. Can I leave it as it looks like at the moment (after today's changes)?

}

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

return $concat;
}

public function getExistentClasses(String_ $string): array
{
// @see https://regex101.com/r/Vv41Qr/1/
$matches = Strings::matchAll($string->value, '#([\\\\a-zA-Z0-9_\\x80-\\xff]*)::#', PREG_PATTERN_ORDER);

return array_filter($matches[1], function (string $className) {
return class_exists($className);
});
}

public function getParts(String_ $string, array $classNames): array
{
$classNames = array_map(function (string $className) {
return preg_quote($className);
}, $classNames);

// @see https://regex101.com/r/8nGS0F/1
$parts = Strings::split($string->value, '#(' . implode('|', $classNames) . ')#');

return array_filter($parts, function (string $className) {
return $className !== '';
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\String_\UseClassKeywordForClassNameResolutionRector\Fixture;

class MultipleOccurrencesClass
{
public function run()
{
return 'Rector\CodingStyle\Tests\Rector\String_\UseClassKeywordForClassNameResolutionRector\Fixture\MultipleOccurrencesClass::staticCall() && Rector\CodingStyle\Tests\Rector\String_\UseClassKeywordForClassNameResolutionRector\Fixture\MultipleOccurrencesClass::staticCall()';
}
}

?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\String_\UseClassKeywordForClassNameResolutionRector\Fixture;

class MultipleOccurrencesClass
{
public function run()
{
return \Rector\CodingStyle\Tests\Rector\String_\UseClassKeywordForClassNameResolutionRector\Fixture\MultipleOccurrencesClass::class . '::staticCall() && ' . \Rector\CodingStyle\Tests\Rector\String_\UseClassKeywordForClassNameResolutionRector\Fixture\MultipleOccurrencesClass::class . '::staticCall()';
}
}

?>