Skip to content
Open
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
25 changes: 25 additions & 0 deletions specs/string-literal/array-var.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@

PHP,

'String argument referencing a namespaced internal class' => <<<'PHP'
<?php

$subclasses['PDO\\Mysql'] = 'PDO\\Mysql';
$subclasses['Random\\Randomizer'] = 'Random\\Randomizer';
$subclasses['Filter\\FilterFailedException'] = 'Filter\\FilterFailedException';

$x = [
'Random\\Randomizer' => 'Random\\Randomizer',
'PDO\\Mysql' => 'PDO\\Mysql',
'Filter\\FilterFailedException' => 'Filter\\FilterFailedException',
];

----
<?php

namespace Humbug;

$subclasses['PDO\Mysql'] = 'PDO\Mysql';
$subclasses['Random\Randomizer'] = 'Random\Randomizer';
$subclasses['Filter\FilterFailedException'] = 'Filter\FilterFailedException';
$x = ['Random\Randomizer' => 'Random\Randomizer', 'PDO\Mysql' => 'PDO\Mysql', 'Filter\FilterFailedException' => 'Filter\FilterFailedException'];

PHP,

'Array item of a list with class-like symbols' => <<<'PHP'
<?php

Expand Down
33 changes: 33 additions & 0 deletions specs/string-literal/new.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,39 @@

PHP,

'FQCN string argument referencing a namespaced internal class' => <<<'PHP'
<?php

new X('Random\\Randomizer');
new X('\\Random\\Randomizer');
new X('PDO\\Mysql');
new X('\\PDO\\Mysql');
new X('Pdo\\Sqlite');
new X('Filter\\FilterFailedException');
new X('\\Filter\\FilterFailedException');

new X('Symfony\\Component\\Yaml\\Ya_1');
new X('Yaml');
new DateTime('now');

----
<?php

namespace Humbug;

new X('Random\Randomizer');
new X('\Random\Randomizer');
new X('PDO\Mysql');
new X('\PDO\Mysql');
new X('Pdo\Sqlite');
new X('Filter\FilterFailedException');
new X('\Filter\FilterFailedException');
new X('Humbug\Symfony\Component\Yaml\Ya_1');
new X('Yaml');
new \DateTime('now');

PHP,

'FQCN string argument on exposed class' => SpecWithConfig::create(
exposeClasses: ['Symfony\Component\Yaml\Yaml'],
spec: <<<'PHP'
Expand Down
22 changes: 22 additions & 0 deletions specs/string-literal/var.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@

PHP,

'FQCN string argument referencing a namespaced internal class' => <<<'PHP'
<?php

$x = 'Random\\Randomizer';
$x = '\\Random\\Randomizer';
$x = 'PDO\\Mysql';
$x = '\\PDO\\Mysql';
$x = 'Filter\\FilterFailedException';

----
<?php

namespace Humbug;

$x = 'Random\Randomizer';
$x = '\Random\Randomizer';
$x = 'PDO\Mysql';
$x = '\PDO\Mysql';
$x = 'Filter\FilterFailedException';

PHP,

'Invalid FQCN strings' => <<<'PHP'
<?php

Expand Down
16 changes: 10 additions & 6 deletions src/PhpParser/NodeVisitor/StringScalarPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ private function prefixStringScalar(String_ $string): String_

// If belongs to the global namespace then we cannot differentiate the
// value from a symbol and a regular string hence we leave it alone
return $this->belongsToTheGlobalNamespace($string)
? $string
: $this->createPrefixedString($string);
return $this->createPrefixedStringIfDoesNotBelongToGlobalNamespace($string);
}

private function prefixStringArg(String_ $string, Arg $parentNode, string $normalizedValue): String_
Expand Down Expand Up @@ -379,9 +377,15 @@ private static function isConstantNode(String_ $node): bool
private function createPrefixedStringIfDoesNotBelongToGlobalNamespace(String_ $string): String_
{
// If belongs to the global namespace then we cannot differentiate the value from a symbol and a regular string
return $this->belongsToTheGlobalNamespace($string)
? $string
: $this->createPrefixedString($string);
if ($this->belongsToTheGlobalNamespace($string)) {
return $string;
}

if ($this->enrichedReflector->isClassExcluded(ltrim($string->value, '\\'))) {
return $string;
}

return $this->createPrefixedString($string);
}

private function belongsToTheGlobalNamespace(String_ $string): bool
Expand Down
Loading