Test Failed
Push — main ( ea931b...8f4107 )
by Bingo
06:03
created

ScriptUtil   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 38
c 1
b 0
f 0
dl 0
loc 132
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isDynamicScriptExpression() 0 3 3
A getScriptFromSource() 0 11 2
A getScriptFromResource() 0 11 2
A getScriptFromResourceExpression() 0 5 1
A getScriptFactory() 0 7 2
A getScript() 0 9 2
A getScriptFromSourceExpression() 0 5 1
1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
use Jabe\Engine\Delegate\ExpressionInterface;
6
use Jabe\Engine\Exception\NotValidException;
7
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
8
use Jabe\Engine\Impl\Context\Context;
9
use Jabe\Engine\Impl\El\ExpressionManager;
10
use Jabe\Engine\Impl\Scripting\{
11
    ExecutableScript,
12
    ScriptFactory
13
};
14
use Jabe\Engine\Impl\Scripting\Engine\JuelScriptEngineFactory;
15
use Jabe\Engine\Impl\Util\EnsureUtil;
16
17
class ScriptUtil
18
{
19
    /**
20
     * Creates a new {@link ExecutableScript} from a source or resource. It excepts static and
21
     * dynamic sources and resources. Dynamic means that the source or resource is an expression
22
     * which will be evaluated during execution.
23
     *
24
     * @param language the language of the script
25
     * @param source the source code of the script or an expression which evaluates to the source code
26
     * @param resource the resource path of the script code or an expression which evaluates to the resource path
27
     * @param expressionManager the expression manager to use to generate the expressions of dynamic scripts
28
     * @return the newly created script
29
     * @throws NotValidException if language is null or empty or both of source and resource are null or empty
30
     */
31
    public static function getScript(string $language, string $source, ?string $resource, ?ExpressionManager $expressionManager): ExecutableScript
32
    {
33
        $scriptFactory = self::getScriptFactory();
34
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language);
0 ignored issues
show
Bug introduced by
$language 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

34
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", /** @scrutinizer ignore-type */ $language);
Loading history...
35
        EnsureUtil::ensureAtLeastOneNotNull("No script source or resource was given", $source, $resource);
36
        if (!empty($resource)) {
37
            return self::getScriptFromResource($language, $resource, $expressionManager, $scriptFactory);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getScriptFr...anager, $scriptFactory) returns the type Jabe\Engine\Impl\Scripti...esourceExecutableScript which is incompatible with the documented return type Jabe\Engine\Impl\Util\the.
Loading history...
Bug introduced by
$language of type array is incompatible with the type string expected by parameter $language of Jabe\Engine\Impl\Util\Sc...getScriptFromResource(). ( Ignorable by Annotation )

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

37
            return self::getScriptFromResource(/** @scrutinizer ignore-type */ $language, $resource, $expressionManager, $scriptFactory);
Loading history...
38
        } else {
39
            return self::getScriptFormSource($language, $source, $expressionManager, $scriptFactory);
0 ignored issues
show
Bug introduced by
The method getScriptFormSource() does not exist on Jabe\Engine\Impl\Util\ScriptUtil. Did you maybe mean getScript()? ( Ignorable by Annotation )

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

39
            return self::/** @scrutinizer ignore-call */ getScriptFormSource($language, $source, $expressionManager, $scriptFactory);

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...
40
        }
41
    }
42
43
    /**
44
     * Creates a new {@link ExecutableScript} from a source. It excepts static and dynamic sources.
45
     * Dynamic means that the source is an expression which will be evaluated during execution.
46
     *
47
     * @param language the language of the script
48
     * @param source the source code of the script or an expression which evaluates to the source code
49
     * @param expressionManager the expression manager to use to generate the expressions of dynamic scripts
50
     * @param scriptFactory the script factory used to create the script
51
     * @return the newly created script
52
     * @throws NotValidException if language is null or empty or source is null
53
     */
54
    public static function getScriptFromSource(string $language, string $source, ?ExpressionManager $expressionManager, ScriptFactory $scriptFactory): ExecutableScript
55
    {
56
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language);
0 ignored issues
show
Bug introduced by
$language 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

56
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", /** @scrutinizer ignore-type */ $language);
Loading history...
57
        EnsureUtil::ensureNotNull(NotValidException::class, "Script source", $source);
58
        if (self::isDynamicScriptExpression($language, $source)) {
0 ignored issues
show
Bug introduced by
$language of type array is incompatible with the type null|string expected by parameter $language of Jabe\Engine\Impl\Util\Sc...namicScriptExpression(). ( Ignorable by Annotation )

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

58
        if (self::isDynamicScriptExpression(/** @scrutinizer ignore-type */ $language, $source)) {
Loading history...
59
            $sourceExpression = $expressionManager->createExpression($source);
0 ignored issues
show
Bug introduced by
The method createExpression() does not exist on null. ( Ignorable by Annotation )

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

59
            /** @scrutinizer ignore-call */ 
60
            $sourceExpression = $expressionManager->createExpression($source);

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...
60
            return self::getScriptFromSourceExpression($language, $sourceExpression, $scriptFactory);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getScriptFr...ession, $scriptFactory) returns the type Jabe\Engine\Impl\Scripti...cSourceExecutableScript which is incompatible with the documented return type Jabe\Engine\Impl\Util\the.
Loading history...
Bug introduced by
$language of type array is incompatible with the type string expected by parameter $language of Jabe\Engine\Impl\Util\Sc...tFromSourceExpression(). ( Ignorable by Annotation )

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

60
            return self::getScriptFromSourceExpression(/** @scrutinizer ignore-type */ $language, $sourceExpression, $scriptFactory);
Loading history...
61
        } else {
62
            EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language);
63
            EnsureUtil::ensureNotNull(NotValidException::class, "Script source", $source);
64
            return $scriptFactory->createScriptFromSource($language, $source);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $scriptFactory->c...rce($language, $source) returns the type Jabe\Engine\Impl\Scripting\SourceExecutableScript which is incompatible with the documented return type Jabe\Engine\Impl\Util\the.
Loading history...
Bug introduced by
$language of type array is incompatible with the type string expected by parameter $language of Jabe\Engine\Impl\Scripti...reateScriptFromSource(). ( Ignorable by Annotation )

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

64
            return $scriptFactory->createScriptFromSource(/** @scrutinizer ignore-type */ $language, $source);
Loading history...
65
        }
66
    }
67
68
    /**
69
     * Creates a new {@link ExecutableScript} from a dynamic source. Dynamic means that the source
70
     * is an expression which will be evaluated during execution.
71
     *
72
     * @param language the language of the script
73
     * @param sourceExpression the expression which evaluates to the source code
74
     * @param scriptFactory the script factory used to create the script
75
     * @return the newly created script
76
     * @throws NotValidException if language is null or empty or sourceExpression is null
77
     */
78
    public static function getScriptFromSourceExpression(string $language, ExpressionInterface $sourceExpression, ScriptFactory $scriptFactory): ExecutableScript
79
    {
80
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language);
0 ignored issues
show
Bug introduced by
$language 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

80
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", /** @scrutinizer ignore-type */ $language);
Loading history...
81
        EnsureUtil::ensureNotNull(NotValidException::class, "Script source expression", $sourceExpression);
82
        return $scriptFactory->createScriptFromSource($language, $sourceExpression);
0 ignored issues
show
Bug introduced by
$language of type array is incompatible with the type string expected by parameter $language of Jabe\Engine\Impl\Scripti...reateScriptFromSource(). ( Ignorable by Annotation )

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

82
        return $scriptFactory->createScriptFromSource(/** @scrutinizer ignore-type */ $language, $sourceExpression);
Loading history...
Bug Best Practice introduced by
The expression return $scriptFactory->c...age, $sourceExpression) returns the type Jabe\Engine\Impl\Scripti...cSourceExecutableScript which is incompatible with the documented return type Jabe\Engine\Impl\Util\the.
Loading history...
83
    }
84
85
    /**
86
     * Creates a new {@link ExecutableScript} from a resource. It excepts static and dynamic resources.
87
     * Dynamic means that the resource is an expression which will be evaluated during execution.
88
     *
89
     * @param language the language of the script
90
     * @param resource the resource path of the script code or an expression which evaluates to the resource path
91
     * @param expressionManager the expression manager to use to generate the expressions of dynamic scripts
92
     * @param scriptFactory the script factory used to create the script
93
     * @return the newly created script
94
     * @throws NotValidException if language or resource are null or empty
95
     */
96
    public static function getScriptFromResource(string $language, string $resource, ?ExpressionManager $expressionManager, ?ScriptFactory $scriptFactory): ExecutableScript
97
    {
98
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language);
0 ignored issues
show
Bug introduced by
$language 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

98
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", /** @scrutinizer ignore-type */ $language);
Loading history...
99
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script resource", $resource);
100
        if (self::isDynamicScriptExpression($language, $resource)) {
0 ignored issues
show
Bug introduced by
$language of type array is incompatible with the type null|string expected by parameter $language of Jabe\Engine\Impl\Util\Sc...namicScriptExpression(). ( Ignorable by Annotation )

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

100
        if (self::isDynamicScriptExpression(/** @scrutinizer ignore-type */ $language, $resource)) {
Loading history...
Bug introduced by
$resource of type array is incompatible with the type string expected by parameter $value of Jabe\Engine\Impl\Util\Sc...namicScriptExpression(). ( Ignorable by Annotation )

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

100
        if (self::isDynamicScriptExpression($language, /** @scrutinizer ignore-type */ $resource)) {
Loading history...
101
            $resourceExpression = $expressionManager->createExpression($resource);
0 ignored issues
show
Bug introduced by
$resource of type array is incompatible with the type string expected by parameter $expression of Jabe\Engine\Impl\El\Expr...ger::createExpression(). ( Ignorable by Annotation )

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

101
            $resourceExpression = $expressionManager->createExpression(/** @scrutinizer ignore-type */ $resource);
Loading history...
102
            return self::getScriptFromResourceExpression($language, $resourceExpression, $scriptFactory);
0 ignored issues
show
Bug introduced by
It seems like $scriptFactory can also be of type null; however, parameter $scriptFactory of Jabe\Engine\Impl\Util\Sc...romResourceExpression() does only seem to accept Jabe\Engine\Impl\Scripting\ScriptFactory, maybe add an additional type check? ( Ignorable by Annotation )

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

102
            return self::getScriptFromResourceExpression($language, $resourceExpression, /** @scrutinizer ignore-type */ $scriptFactory);
Loading history...
Bug Best Practice introduced by
The expression return self::getScriptFr...ession, $scriptFactory) returns the type Jabe\Engine\Impl\Scripti...esourceExecutableScript which is incompatible with the documented return type Jabe\Engine\Impl\Util\the.
Loading history...
Bug introduced by
$language of type array is incompatible with the type string expected by parameter $language of Jabe\Engine\Impl\Util\Sc...romResourceExpression(). ( Ignorable by Annotation )

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

102
            return self::getScriptFromResourceExpression(/** @scrutinizer ignore-type */ $language, $resourceExpression, $scriptFactory);
Loading history...
103
        } else {
104
            EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language);
105
            EnsureUtil::ensureNotEmpty(NotValidException::class, "Script resource", $resource);
106
            return $scriptFactory->createScriptFromResource($language, $resource);
0 ignored issues
show
Bug introduced by
$language of type array is incompatible with the type string expected by parameter $language of Jabe\Engine\Impl\Scripti...ateScriptFromResource(). ( Ignorable by Annotation )

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

106
            return $scriptFactory->createScriptFromResource(/** @scrutinizer ignore-type */ $language, $resource);
Loading history...
Bug introduced by
The method createScriptFromResource() does not exist on null. ( Ignorable by Annotation )

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

106
            return $scriptFactory->/** @scrutinizer ignore-call */ createScriptFromResource($language, $resource);

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...
Bug Best Practice introduced by
The expression return $scriptFactory->c...e($language, $resource) returns the type Jabe\Engine\Impl\Scripti...esourceExecutableScript which is incompatible with the documented return type Jabe\Engine\Impl\Util\the.
Loading history...
107
        }
108
    }
109
110
    /**
111
     * Creates a new {@link ExecutableScript} from a dynamic resource. Dynamic means that the source
112
     * is an expression which will be evaluated during execution.
113
     *
114
     * @param language the language of the script
115
     * @param resourceExpression the expression which evaluates to the resource path
116
     * @param scriptFactory the script factory used to create the script
117
     * @return the newly created script
118
     * @throws NotValidException if language is null or empty or resourceExpression is null
119
     */
120
    public static function getScriptFromResourceExpression(string $language, ExpressionInterface $resourceExpression, ScriptFactory $scriptFactory): ExecutableScript
121
    {
122
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language);
0 ignored issues
show
Bug introduced by
$language 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

122
        EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", /** @scrutinizer ignore-type */ $language);
Loading history...
123
        EnsureUtil::ensureNotNull(NotValidException::class, "Script resource expression", $resourceExpression);
124
        return $scriptFactory->createScriptFromResource($language, $resourceExpression);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $scriptFactory->c...e, $resourceExpression) returns the type Jabe\Engine\Impl\Scripti...esourceExecutableScript which is incompatible with the documented return type Jabe\Engine\Impl\Util\the.
Loading history...
Bug introduced by
$language of type array is incompatible with the type string expected by parameter $language of Jabe\Engine\Impl\Scripti...ateScriptFromResource(). ( Ignorable by Annotation )

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

124
        return $scriptFactory->createScriptFromResource(/** @scrutinizer ignore-type */ $language, $resourceExpression);
Loading history...
125
    }
126
127
    /**
128
     * Checks if the value is an expression for a dynamic script source or resource.
129
     *
130
     * @param language the language of the script
131
     * @param value the value to check
132
     * @return true if the value is an expression for a dynamic script source/resource, otherwise false
133
     */
134
    public static function isDynamicScriptExpression(?string $language, string $value): bool
135
    {
136
        return StringUtil::isExpression($value) && $language != null && !in_array(strtolower($language), JuelScriptEngineFactory::$names);
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $language of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
Bug Best Practice introduced by
The expression return Jabe\Engine\Impl\...ptEngineFactory::names) returns the type boolean which is incompatible with the documented return type true.
Loading history...
137
    }
138
139
    /**
140
     * Returns the configured script factory in the context or a new one.
141
     */
142
    public static function getScriptFactory(): ScriptFactory
143
    {
144
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
145
        if ($processEngineConfiguration !== null) {
146
            return $processEngineConfiguration->getScriptFactory();
0 ignored issues
show
Bug introduced by
The method getScriptFactory() 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

146
            return $processEngineConfiguration->/** @scrutinizer ignore-call */ getScriptFactory();

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...
147
        } else {
148
            return new ScriptFactory();
149
        }
150
    }
151
}
152