Passed
Push — trunk ( 0e4c2d...148418 )
by Christian
12:04 queued 12s
created

PHPUnitDataProviderStaticRule::processNode()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 30
rs 9.4888
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\Tests;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Stmt\ClassMethod;
7
use PHPStan\Analyser\Scope;
0 ignored issues
show
Bug introduced by
The type PHPStan\Analyser\Scope was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPStan\Rules\Rule;
0 ignored issues
show
Bug introduced by
The type PHPStan\Rules\Rule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use PHPStan\ShouldNotHappenException;
0 ignored issues
show
Bug introduced by
The type PHPStan\ShouldNotHappenException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Shopware\Core\Framework\Log\Package;
11
12
/**
13
 * @implements Rule<Node\Stmt\ClassMethod>
14
 *
15
 * @internal
16
 */
17
#[Package('core')]
18
class PHPUnitDataProviderStaticRule implements Rule
19
{
20
    public function getNodeType(): string
21
    {
22
        return ClassMethod::class;
23
    }
24
25
    /**
26
     * @param Node\Stmt\ClassMethod $node
27
     *
28
     * @return string[]
29
     */
30
    public function processNode(Node $node, Scope $scope): array
31
    {
32
        // Check if the method has a dataProvider annotation
33
        $docComment = $node->getDocComment();
34
        if ($docComment === null) {
35
            return [];
36
        }
37
38
        $dataProviderPattern = '/@dataProvider\s+([^\s]+)/';
39
        if (!preg_match($dataProviderPattern, $docComment->getText(), $matches)) {
40
            return [];
41
        }
42
43
        // Get the dataProvider method name
44
        $dataProviderName = $matches[1];
45
46
        // Find the dataProvider method in the same class
47
        $classReflection = $scope->getClassReflection();
48
        if ($classReflection === null) {
49
            throw new ShouldNotHappenException();
50
        }
51
52
        $dataProviderMethod = $classReflection->getNativeMethod($dataProviderName);
53
54
        // Check if the dataProvider method is static
55
        if (!$dataProviderMethod->isStatic()) {
56
            return [sprintf('DataProvider method %s should be static.', $dataProviderName)];
57
        }
58
59
        return [];
60
    }
61
}
62