Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ForbiddenClassNameExtension for append additional forbidden… #2978

Closed
Closed
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
13 changes: 13 additions & 0 deletions src/Classes/ForbiddenClassNameExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);


namespace PHPStan\Classes;


interface ForbiddenClassNameExtension
{
public const EXTENSION_TAG = 'phpstan.forbiddenClassNamesExtension';

/** @return array<string, string> */
public function getClassPrefixes(): array;
}
17 changes: 16 additions & 1 deletion src/Rules/ClassForbiddenNameCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Rules;

use PHPStan\Classes\ForbiddenClassNameExtension;
use PHPStan\DependencyInjection\Container;
use function sprintf;
use function str_starts_with;
use function strpos;
Expand All @@ -16,18 +18,31 @@
'PHP-Scoper' => '_PhpScoper',
];

public function __construct(private Container $container)
{
}

/**
* @param ClassNameNodePair[] $pairs
* @return RuleError[]
*/
public function checkClassNames(array $pairs): array
{
$extensions = $this->container->getServicesByTag(ForbiddenClassNameExtension::EXTENSION_TAG);

$classPrefixes = array_merge(
self::INTERNAL_CLASS_PREFIXES,
...array_map(static function (ForbiddenClassNameExtension $extension): array {
return $extension->getForbiddenClassNames();

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 36 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().
}, $extensions),
);

Check failure on line 38 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 38 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 38 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 38 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 39 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 39 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 39 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().
$errors = [];
foreach ($pairs as $pair) {

Check failure on line 41 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 41 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 41 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().

Check failure on line 41 in src/Rules/ClassForbiddenNameCheck.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Call to an undefined method PHPStan\Classes\ForbiddenClassNameExtension::getForbiddenClassNames().
$className = $pair->getClassName();

$projectName = null;
foreach (self::INTERNAL_CLASS_PREFIXES as $project => $prefix) {
foreach ($classPrefixes as $project => $prefix) {
if (str_starts_with($className, $prefix)) {
$projectName = $project;
break;
Expand Down
Loading