-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error on references of prefixed internal class names from PHAR files
- Loading branch information
Showing
65 changed files
with
719 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
use function sprintf; | ||
use function str_starts_with; | ||
use function strpos; | ||
use function substr; | ||
|
||
class ClassForbiddenNameCheck | ||
{ | ||
|
||
private const INTERNAL_CLASS_PREFIXES = [ | ||
'PHPStan' => '_PHPStan_', | ||
'Rector' => 'RectorPrefix', | ||
'PHP-Scoper' => '_PhpScoper', | ||
]; | ||
|
||
/** | ||
* @param ClassNameNodePair[] $pairs | ||
* @return RuleError[] | ||
*/ | ||
public function checkClassNames(array $pairs): array | ||
{ | ||
$errors = []; | ||
foreach ($pairs as $pair) { | ||
$className = $pair->getClassName(); | ||
|
||
$projectName = null; | ||
foreach (self::INTERNAL_CLASS_PREFIXES as $project => $prefix) { | ||
if (str_starts_with($className, $prefix)) { | ||
$projectName = $project; | ||
break; | ||
} | ||
} | ||
|
||
if ($projectName === null) { | ||
continue; | ||
} | ||
|
||
$error = RuleErrorBuilder::message(sprintf( | ||
'Referencing prefixed %s class: %s.', | ||
$projectName, | ||
$className, | ||
))->line($pair->getNode()->getLine())->nonIgnorable(); | ||
|
||
if (strpos($className, '\\') !== false) { | ||
$error->tip(sprintf( | ||
'This is most likely unintentional. Did you mean to type %s?', | ||
substr($className, strpos($className, '\\')), | ||
)); | ||
} | ||
|
||
$errors[] = $error->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
class ClassNameCheck | ||
{ | ||
|
||
public function __construct( | ||
private ClassCaseSensitivityCheck $classCaseSensitivityCheck, | ||
private ClassForbiddenNameCheck $classForbiddenNameCheck, | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @param ClassNameNodePair[] $pairs | ||
* @return RuleError[] | ||
*/ | ||
public function checkClassNames(array $pairs, bool $checkClassCaseSensitivity = true): array | ||
{ | ||
$errors = []; | ||
|
||
if ($checkClassCaseSensitivity) { | ||
foreach ($this->classCaseSensitivityCheck->checkClassNames($pairs) as $error) { | ||
$errors[] = $error; | ||
} | ||
} | ||
foreach ($this->classForbiddenNameCheck->checkClassNames($pairs) as $error) { | ||
$errors[] = $error; | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.