Passed
Push — trunk ( 10200d...995d50 )
by Christian
11:59 queued 12s
created

RuleConditionHasRuleConfigRule   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
dl 0
loc 94
rs 10
c 1
b 0
f 0
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isRuleClass() 0 13 4
A processNode() 0 11 6
A isValid() 0 10 3
A isAllowed() 0 8 2
A getNodeType() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules;
4
5
use PhpParser\Node;
6
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...
7
use PHPStan\Node\InClassNode;
0 ignored issues
show
Bug introduced by
The type PHPStan\Node\InClassNode 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\Rules\RuleError;
0 ignored issues
show
Bug introduced by
The type PHPStan\Rules\RuleError 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\Checkout\Cart\Rule\AlwaysValidRule;
11
use Shopware\Core\Checkout\Cart\Rule\GoodsCountRule;
12
use Shopware\Core\Checkout\Cart\Rule\GoodsPriceRule;
13
use Shopware\Core\Checkout\Cart\Rule\LineItemCustomFieldRule;
14
use Shopware\Core\Checkout\Cart\Rule\LineItemGoodsTotalRule;
15
use Shopware\Core\Checkout\Cart\Rule\LineItemGroupRule;
16
use Shopware\Core\Checkout\Cart\Rule\LineItemInCategoryRule;
17
use Shopware\Core\Checkout\Cart\Rule\LineItemPropertyRule;
18
use Shopware\Core\Checkout\Cart\Rule\LineItemPurchasePriceRule;
19
use Shopware\Core\Checkout\Cart\Rule\LineItemRule;
20
use Shopware\Core\Checkout\Cart\Rule\LineItemsInCartRule;
21
use Shopware\Core\Checkout\Cart\Rule\LineItemWithQuantityRule;
22
use Shopware\Core\Checkout\Cart\Rule\LineItemWrapperRule;
23
use Shopware\Core\Checkout\Customer\Rule\BillingZipCodeRule;
24
use Shopware\Core\Checkout\Customer\Rule\CustomerCustomFieldRule;
25
use Shopware\Core\Checkout\Customer\Rule\ShippingZipCodeRule;
26
use Shopware\Core\Framework\Rule\Container\AndRule;
27
use Shopware\Core\Framework\Rule\Container\Container;
28
use Shopware\Core\Framework\Rule\Container\FilterRule;
29
use Shopware\Core\Framework\Rule\Container\MatchAllLineItemsRule;
30
use Shopware\Core\Framework\Rule\Container\NotRule;
31
use Shopware\Core\Framework\Rule\Container\OrRule;
32
use Shopware\Core\Framework\Rule\Container\XorRule;
33
use Shopware\Core\Framework\Rule\Container\ZipCodeRule;
34
use Shopware\Core\Framework\Rule\DateRangeRule;
35
use Shopware\Core\Framework\Rule\Rule as ShopwareRule;
36
use Shopware\Core\Framework\Rule\ScriptRule;
37
use Shopware\Core\Framework\Rule\SimpleRule;
38
use Shopware\Core\Framework\Rule\TimeRangeRule;
39
40
/**
41
 * @implements Rule<InClassNode>
42
 *
43
 * @internal
44
 */
45
class RuleConditionHasRuleConfigRule implements Rule
46
{
47
    /**
48
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type Shopware\Core\DevOps\Sta...lyze\PHPStan\Rules\list 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...
49
     */
50
    private array $rulesAllowedToBeWithoutConfig = [
51
        ZipCodeRule::class,
52
        FilterRule::class,
53
        Container::class,
54
        AndRule::class,
55
        NotRule::class,
56
        OrRule::class,
57
        XorRule::class,
58
        MatchAllLineItemsRule::class,
59
        ScriptRule::class,
60
        DateRangeRule::class,
61
        SimpleRule::class,
62
        TimeRangeRule::class,
63
        GoodsCountRule::class,
64
        GoodsPriceRule::class,
65
        LineItemRule::class,
66
        LineItemsInCartRule::class,
67
        LineItemWithQuantityRule::class,
68
        LineItemWrapperRule::class,
69
        BillingZipCodeRule::class,
70
        ShippingZipCodeRule::class,
71
        AlwaysValidRule::class,
72
        LineItemPropertyRule::class,
73
        LineItemPurchasePriceRule::class,
74
        LineItemInCategoryRule::class,
75
        LineItemCustomFieldRule::class,
76
        LineItemGoodsTotalRule::class,
77
        CustomerCustomFieldRule::class,
78
        LineItemGroupRule::class,
79
    ];
80
81
    public function getNodeType(): string
82
    {
83
        return InClassNode::class;
84
    }
85
86
    /**
87
     * @param InClassNode $node
88
     *
89
     * @return array<array-key, RuleError|string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, RuleError|string> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, RuleError|string>.
Loading history...
90
     */
91
    public function processNode(Node $node, Scope $scope): array
92
    {
93
        if (!$this->isRuleClass($scope) || $this->isAllowed($scope) || $this->isValid($scope)) {
94
            if ($this->isAllowed($scope) && $this->isValid($scope)) {
95
                return ['This class is implementing the getConfig function and has a own admin component. Remove getConfig or the component.'];
96
            }
97
98
            return [];
99
        }
100
101
        return ['This class has to implement getConfig or implement a new admin component.'];
102
    }
103
104
    private function isValid(Scope $scope): bool
105
    {
106
        $class = $scope->getClassReflection();
107
        if ($class === null || !$class->hasMethod('getConfig')) {
108
            return false;
109
        }
110
111
        $declaringClass = $class->getMethod('getConfig', $scope)->getDeclaringClass();
112
113
        return $declaringClass->getName() !== ShopwareRule::class;
114
    }
115
116
    private function isAllowed(Scope $scope): bool
117
    {
118
        $class = $scope->getClassReflection();
119
        if ($class === null) {
120
            return false;
121
        }
122
123
        return \in_array($class->getName(), $this->rulesAllowedToBeWithoutConfig, true);
124
    }
125
126
    private function isRuleClass(Scope $scope): bool
127
    {
128
        $class = $scope->getClassReflection();
129
        if ($class === null) {
130
            return false;
131
        }
132
133
        $namespace = $class->getName();
134
        if (!\str_contains($namespace, 'Shopware\\Tests\\Unit\\') && !\str_contains($namespace, 'Shopware\\Tests\\Migration\\')) {
135
            return false;
136
        }
137
138
        return $class->isSubclassOf(ShopwareRule::class);
139
    }
140
}
141