Test Failed
Push — main ( f68dd4...f46f5c )
by Bingo
14:33
created

ClassDelegateUtil   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 26
c 1
b 0
f 1
dl 0
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A instantiateDelegate() 0 12 2
A fieldTypeCompatible() 0 9 1
A applyFieldDeclaration() 0 26 6
1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
use Jabe\Engine\ArtifactFactoryInterface;
6
use Jabe\Engine\Impl\ProcessEngineLogger;
7
use Jabe\Engine\Impl\Bpmn\Parser\FieldDeclaration;
8
use Jabe\Engine\Impl\Context\Context;
9
use Jabe\Engine\Impl\Util\EnsureUtil;
10
11
class ClassDelegateUtil
12
{
13
    //private static final EngineUtilLogger LOG = ProcessEngineLogger.UTIL_LOGGER;
14
15
    public static function instantiateDelegate(string $clazz, array $fieldDeclarations)
16
    {
17
        $artifactFactory = Context::getProcessEngineConfiguration()->getArtifactFactory();
0 ignored issues
show
Bug introduced by
The method getArtifactFactory() 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

17
        $artifactFactory = Context::getProcessEngineConfiguration()->/** @scrutinizer ignore-call */ getArtifactFactory();

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...
18
19
        try {
20
            $object = $artifactFactory->getArtifact($clazz);
21
22
            self::applyFieldDeclaration($fieldDeclarations, $object);
23
            return $object;
24
        } catch (\Exception $e) {
25
            //throw LOG.exceptionWhileInstantiatingClass(className, e);
26
            throw new \Exception("exceptionWhileInstantiatingClass");
27
        }
28
    }
29
30
    public static function applyFieldDeclaration($fieldDeclarations, $target): void
31
    {
32
        if (is_array($fieldDeclarations)) {
33
            foreach ($fieldDeclarations as $declaration) {
34
                self::applyFieldDeclaration($declaration, $target);
35
            }
36
        } else {
37
            $declaration = $fieldDeclarations;
38
            $setterMethod = ReflectUtil::getSetter($declaration->getName(), get_class($target));
39
40
            if ($setterMethod != null) {
41
                try {
42
                    $setterMethod->invoke($target, $declaration->getValue());
43
                } catch (\Exception $e) {
44
                    //throw LOG.exceptionWhileApplyingFieldDeclatation(declaration.getName(), target.getClass().getName(), e);
45
                    throw new \Exception("exceptionWhileApplyingFieldDeclatation");
46
                }
47
            } else {
48
                $field = ReflectUtil::getField($declaration->getName(), $target);
49
                EnsureUtil::ensureNotNull("Field definition uses unexisting field '" . $declaration->getName() . "' on class " . get_class($target), "field", $field);
50
                // Check if the delegate field's type is correct
51
                if (!self::fieldTypeCompatible($declaration, $field)) {
52
                    //throw LOG.incompatibleTypeForFieldDeclaration(declaration, target, field);
53
                    throw new \Exception("incompatibleTypeForFieldDeclaration");
54
                }
55
                ReflectUtil::setField($field, $target, $declaration->getValue());
56
            }
57
        }
58
    }
59
60
    public static function fieldTypeCompatible(FieldDeclaration $declaration, \ReflectionProperty $field): bool
0 ignored issues
show
Unused Code introduced by
The parameter $declaration 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

60
    public static function fieldTypeCompatible(/** @scrutinizer ignore-unused */ FieldDeclaration $declaration, \ReflectionProperty $field): bool

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...
Unused Code introduced by
The parameter $field 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

60
    public static function fieldTypeCompatible(FieldDeclaration $declaration, /** @scrutinizer ignore-unused */ \ReflectionProperty $field): bool

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...
61
    {
62
        /*if ($declaration->getValue() !== null) {
63
            return field.getType().isAssignableFrom(declaration.getValue().getClass());
64
        } else {
65
            // Null can be set any field type
66
            return true;
67
        }*/
68
        return true;
69
    }
70
}
71