Test Failed
Push — main ( 2f8de4...07b5fa )
by Bingo
15:07
created

anager.php$0   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 2
dl 0
loc 11
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace Jabe\Engine\Impl\El;
4
5
use Jabe\Engine\Delegate\VariableScopeInterface;
6
use Jabe\Engine\Impl\Core\Variable\Scope\AbstractVariableScope;
7
use Jabe\Engine\Impl\Util\El\{
8
    ArrayELResolver,
9
    CompositeELResolver,
10
    ELContext,
11
    ELResolver,
12
    ExpressionFactory,
13
    FunctionMapper,
14
    ListELResolver,
15
    MapELResolver,
16
    ValueExpression
17
};
18
use Jabe\Engine\Impl\Juel\ExpressionFactoryImpl;
19
use Jabe\Engine\Impl\Util\EnsureUtil;
20
use Jabe\Engine\Test\Mock\MockElResolver;
21
use Jabe\Engine\Variable\Context\VariableContextInterface;
22
23
class JuelExpressionManager implements ExpressionManagerInterface, ElProviderCompatibleInterface
24
{
25
    protected $functions = [];
26
    protected $expressionFactory;
27
    protected $beans = [];
28
    protected $initialized = false;
29
    protected $elResolver;
30
    protected $functionMapper;
31
    // Default implementation (does nothing)
32
    protected $parsingElContext;
33
    protected $elProvider;
34
35
    public function __construct(array $beans = [])
36
    {
37
        // Use the ExpressionFactoryImpl built-in version of juel, with parametrised
38
        // method expressions enabled
39
        $this->expressionFactory = new ExpressionFactoryImpl();
40
        $this->beans = $beans;
41
    }
42
43
    public function createExpression(string $expression): ExpressionInterface
44
    {
45
        $this->ensureInitialized();
46
        $valueExpression = $this->createValueExpression($expression);
47
        return new JuelExpression($valueExpression, $this, $expression);
48
    }
49
50
    public function addFunction(string $name, \ReflectionMethod $function): void
51
    {
52
        EnsureUtil::ensureNotEmpty("name", "name", $name);
0 ignored issues
show
Bug introduced by
$name of type string is incompatible with the type array expected by parameter $variable of Jabe\Engine\Impl\Util\EnsureUtil::ensureNotEmpty(). ( Ignorable by Annotation )

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

52
        EnsureUtil::ensureNotEmpty("name", "name", /** @scrutinizer ignore-type */ $name);
Loading history...
53
        EnsureUtil::ensureNotNull("function", "function", $function);
54
        $this->functions[$name] = $function;
55
    }
56
57
    public function createValueExpression(string $expression): ValueExpression
58
    {
59
        $this->ensureInitialized();
60
        $instance = new \stdClass();
61
        return $this->expressionFactory->createValueExpression($parsingElContext, $expression, $instance);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parsingElContext seems to be never defined.
Loading history...
62
    }
63
64
    public function setExpressionFactory(ExpressionFactory $expressionFactory): void
65
    {
66
        $this->expressionFactory = $expressionFactory;
67
    }
68
69
    public function getElContext(VariableScopeInterface $variableScope): ELContext
70
    {
71
        $this->ensureInitialized();
72
        $elContext = null;
73
        if ($variableScope instanceof AbstractVariableScopeInterface) {
74
            $variableScopeImpl = $variableScope;
75
            $elContext = $variableScopeImpl->getCachedElContext();
0 ignored issues
show
Bug introduced by
The method getCachedElContext() does not exist on Jabe\Engine\Delegate\VariableScopeInterface. It seems like you code against a sub-type of Jabe\Engine\Delegate\VariableScopeInterface such as Jabe\Engine\Impl\Core\Va...e\AbstractVariableScope or Jabe\Engine\Impl\Persistence\Entity\TaskEntity. ( Ignorable by Annotation )

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

75
            /** @scrutinizer ignore-call */ 
76
            $elContext = $variableScopeImpl->getCachedElContext();
Loading history...
76
        }
77
78
        if ($elContext === null) {
79
            $elContext = $this->createElContext($variableScope);
80
            if ($variableScope instanceof AbstractVariableScopeInterface) {
81
                $variableScope->setCachedElContext($elContext);
0 ignored issues
show
Bug introduced by
The method setCachedElContext() does not exist on Jabe\Engine\Delegate\VariableScopeInterface. It seems like you code against a sub-type of Jabe\Engine\Delegate\VariableScopeInterface such as Jabe\Engine\Impl\Core\Va...e\AbstractVariableScope or Jabe\Engine\Impl\Persistence\Entity\TaskEntity. ( Ignorable by Annotation )

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

81
                $variableScope->/** @scrutinizer ignore-call */ 
82
                                setCachedElContext($elContext);
Loading history...
82
            }
83
        }
84
85
        return $elContext;
86
    }
87
88
    protected function createElContext(/*VariableScopeInterface|VariableContextInterface*/$variableScopeOrContext): ProcessEngineElContext
89
    {
90
        if ($variableScopeOrContext instanceof VariableScopeInterface) {
91
            $this->ensureInitialized();
92
            $elContext = new ProcessEngineElContext($this->functionMapper, $this->elResolver);
93
            $elContext->putContext(ExpressionFactory::class, $this->expressionFactory);
94
            $elContext->putContext(VariableScopeInterface::class, $variableScopeOrContext);
95
            return $elContext;
96
        } elseif ($variableScopeOrContext instanceof VariableContextInterface) {
97
            $this->ensureInitialized();
98
            $elContext = new ProcessEngineElContext($this->functionMapper, $this->elResolver);
99
            $elContext->putContext(ExpressionFactory::class, $this->expressionFactory);
100
            $elContext->putContext(VariableContextInterface::class, $variableScopeOrContext);
101
            return $elContext;
102
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Jabe\Engine\Impl\El\ProcessEngineElContext. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
103
    }
104
105
    protected function ensureInitialized(): void
106
    {
107
        if (!$this->initialized) {
108
            $this->elResolver = $this->createElResolver();
109
            $this->functionMapper = $this->createFunctionMapper();
110
            $this->parsingElContext = new ProcessEngineElContext($this->functionMapper);
111
            $this->initialized = true;
112
        }
113
    }
114
115
    protected function createElResolver(VariableScopeInterface $scope = null): ELResolver
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed. ( Ignorable by Annotation )

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

115
    protected function createElResolver(/** @scrutinizer ignore-unused */ VariableScopeInterface $scope = null): ELResolver

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
        $elResolver = new CompositeELResolver();
118
        $elResolver->add(new VariableScopeElResolver());
119
        $elResolver->add(new VariableContextElResolver());
120
        $elResolver->add(new MockElResolver());
121
122
        /*if (!empty($this->beans)) {
123
            // ACT-1102: Also expose all beans in configuration when using standalone
124
            // engine, not
125
            // in spring-context
126
            elResolver.add(new ReadOnlyMapELResolver(beans));
127
        }*/
128
129
        //$elResolver->add(new ProcessApplicationElResolverDelegate());
130
        $elResolver->add(new ArrayELResolver());
131
        $elResolver->add(new ListELResolver());
132
        $elResolver->add(new MapELResolver());
133
        //$elResolver->add(new ProcessApplicationBeanElResolverDelegate());
134
135
        return $elResolver;
136
    }
137
138
    protected function createFunctionMapper(): FunctionMapper
139
    {
140
        $function = $this->functions;
0 ignored issues
show
Unused Code introduced by
The assignment to $function is dead and can be removed.
Loading history...
141
        $functionMapper = new class ($functions) extends FunctionMapper {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $functions does not exist. Did you maybe mean $function?
Loading history...
142
            private $functions;
143
144
            public function __construct(array $functions)
145
            {
146
                $this->functions = $functions;
147
            }
148
149
            public function resolveFunction(string $prefix, string $localName): \ReflectionMethod
150
            {
151
                return $this->functions[$localName];
152
            }
153
        };
154
        return $functionMapper;
155
    }
156
157
    public function toElProvider()
158
    {
159
        if ($this->elProvider == null) {
160
            $this->elProvider = $this->createElProvider();
161
        }
162
        return $this->elProvider;
163
    }
164
165
    protected function createElProvider()
166
    {
167
        return new ProcessEngineJuelElProvider($this);
168
    }
169
}
170