Test Failed
Push — main ( d01f4c...5af89f )
by Bingo
15:56
created

matchVariableNamesIgnoreCase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Jabe\Engine\Impl;
4
5
use Jabe\Engine\Exception\NotValidException;
6
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
7
use Jabe\Engine\Impl\Context\Context;
8
use Jabe\Engine\Impl\Interceptor\{
9
    CommandContext,
10
    CommandExecutorInterface
11
};
12
use Jabe\Engine\Impl\Variable\Serializer\VariableSerializersInterface;
13
use Jabe\Engine\Impl\Util\EnsureUtil;
0 ignored issues
show
Bug introduced by
The type Jabe\Engine\Impl\Util\EnsureUtil 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...
14
use Jabe\Engine\Query\QueryInterface;
15
16
abstract class AbstractVariableQueryImpl extends AbstractQuery
17
{
18
    protected $queryVariableValues = [];
19
20
    protected $variableNamesIgnoreCase;
21
    protected $variableValuesIgnoreCase;
22
23
    public function __construct(CommandExecutorInterface $commandExecutor = null)
24
    {
25
        if ($commandExecutor !== null) {
26
            parent::__construct($commandExecutor);
27
        }
28
    }
29
30
    abstract public function executeCount(CommandContext $commandContext): int;
31
32
    abstract public function executeList(CommandContext $commandContext, Page $page): array;
0 ignored issues
show
Bug introduced by
The type Jabe\Engine\Impl\Page 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...
33
34
    public function variableValueEquals(string $name, $value): QueryInterface
35
    {
36
        $this->addVariable($name, $value, QueryOperator::EQUALS, true);
37
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
38
    }
39
40
    public function variableValueNotEquals(string $name, $value): QueryInterface
41
    {
42
        $this->addVariable($name, $value, QueryOperator::NOT_EQUALS, true);
43
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
44
    }
45
46
    public function variableValueGreaterThan(string $name, $value): QueryInterface
47
    {
48
        $this->addVariable($name, $value, QueryOperator::GREATER_THAN, true);
49
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
50
    }
51
52
    public function variableValueGreaterThanOrEqual(string $name, $value): QueryInterface
53
    {
54
        $this->addVariable($name, $value, QueryOperator::GREATER_THAN_OR_EQUAL, true);
55
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
56
    }
57
58
    public function variableValueLessThan(string $name, $value): QueryInterface
59
    {
60
        $this->addVariable($name, $value, QueryOperator::LESS_THAN, true);
61
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
62
    }
63
64
    public function variableValueLessThanOrEqual(string $name, $value): QueryInterface
65
    {
66
        $this->addVariable($name, $value, QueryOperator::LESS_THAN_OR_EQUAL, true);
67
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
68
    }
69
70
    public function variableValueLike(string $name, string $value): QueryInterface
71
    {
72
        $this->addVariable($name, $value, QueryOperator::LIKE, true);
73
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
74
    }
75
76
    public function variableValueNotLike(string $name, string $value): QueryInterface
77
    {
78
        $this->addVariable($name, $value, QueryOperator::NOT_LIKE, true);
79
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
80
    }
81
82
    public function matchVariableNamesIgnoreCase(): QueryInterface
83
    {
84
        $this->variableNamesIgnoreCase = true;
85
        foreach ($this->getQueryVariableValues() as $variable) {
86
            $variable->setVariableNameIgnoreCase(true);
87
        }
88
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
89
    }
90
91
    public function matchVariableValuesIgnoreCase(): QUeryInterface
92
    {
93
        $this->variableValuesIgnoreCase = true;
94
        foreach ($this->getQueryVariableValues() as $variable) {
95
            $variable->setVariableValueIgnoreCase(true);
96
        }
97
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jabe\Engine\Impl\AbstractVariableQueryImpl which is incompatible with the type-hinted return Jabe\Engine\Query\QueryInterface.
Loading history...
98
    }
99
100
    protected function addVariable(string $name, $value, string $operator, bool $processInstanceScope): void
101
    {
102
        $queryVariableValue = $this->createQueryVariableValue($name, $value, $operator, $processInstanceScope);
103
        $this->queryVariableValues[] = $queryVariableValue;
104
    }
105
106
    protected function createQueryVariableValue(string $name, $value, string $operator, bool $processInstanceScope): QueryVariableValue
107
    {
108
        $this->validateVariable($name, $value, $operator);
109
110
        $shouldMatchVariableValuesIgnoreCase = $this->variableValuesIgnoreCase == true && $value != null && is_string($value);
111
        $shouldMatchVariableNamesIgnoreCase = $this->variableNamesIgnoreCase == true;
112
113
        return new QueryVariableValue($name, $value, $operator, $processInstanceScope, $shouldMatchVariableNamesIgnoreCase, $shouldMatchVariableValuesIgnoreCase);
114
    }
115
116
    protected function validateVariable(string $name, $value, string $operator): void
117
    {
118
        EnsureUtil::ensureNotNull(NotValidException::class, "name", $name);
119
        if ($value == null || $this->isBoolean($value)) {
120
            // Null-values and booleans can only be used in EQUALS and NOT_EQUALS
121
            switch ($operator) {
122
                case QueryOperator::GREATER_THAN:
123
                    throw new NotValidException("Booleans and null cannot be used in 'greater than' condition");
124
                case QueryOperator::LESS_THAN:
125
                    throw new NotValidException("Booleans and null cannot be used in 'less than' condition");
126
                case QueryOperator::GREATER_THAN_OR_EQUAL:
127
                    throw new NotValidException("Booleans and null cannot be used in 'greater than or equal' condition");
128
                case QueryOperator::LESS_THAN_OR_EQUAL:
129
                    throw new NotValidException("Booleans and null cannot be used in 'less than or equal' condition");
130
                case QueryOperator::LIKE:
131
                    throw new NotValidException("Booleans and null cannot be used in 'like' condition");
132
                case QueryOperator::NOT_LIKE:
133
                    throw new NotValidException("Booleans and null cannot be used in 'not like' condition");
134
                default:
135
                    break;
136
            }
137
        }
138
    }
139
140
    private function isBoolean($value = null): bool
141
    {
142
        if ($value == null) {
143
            return false;
144
        }
145
        return is_bool($value);
146
    }
147
148
    protected function ensureVariablesInitialized(): void
149
    {
150
        if (!empty($this->getQueryVariableValues())) {
151
            $processEngineConfiguration = Context::getProcessEngineConfiguration();
152
            $variableSerializers = $processEngineConfiguration->getVariableSerializers();
0 ignored issues
show
Bug introduced by
The method getVariableSerializers() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
            /** @scrutinizer ignore-call */ 
153
            $variableSerializers = $processEngineConfiguration->getVariableSerializers();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
            $dbType = $processEngineConfiguration->getDatabaseType();
154
            foreach ($this->getQueryVariableValues() as $queryVariableValue) {
155
                $queryVariableValue->initialize($variableSerializers, $dbType);
156
            }
157
        }
158
    }
159
160
    public function getQueryVariableValues(): array
161
    {
162
        return $this->queryVariableValues;
163
    }
164
165
    public function isVariableNamesIgnoreCase(): bool
166
    {
167
        return $this->variableNamesIgnoreCase;
168
    }
169
170
    public function isVariableValuesIgnoreCase(): bool
171
    {
172
        return $this->variableValuesIgnoreCase;
173
    }
174
}
175