GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 810b23...1b23c8 )
by joseph
12s
created

EntityFieldSetter::setEntityHasField()   A

Complexity

Conditions 2
Paths 12

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 2.0227

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 23
cts 28
cp 0.8214
rs 9.392
c 0
b 0
f 0
cc 2
nc 12
nop 2
crap 2.0227
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper;
6
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
7
use gossi\codegen\model\PhpClass;
8
use gossi\codegen\model\PhpInterface;
9
use gossi\codegen\model\PhpTrait;
10
11
class EntityFieldSetter
12
{
13
    /**
14
     * @var CodeHelper
15
     */
16
    protected $codeHelper;
17
18 41
    public function __construct(CodeHelper $codeHelper)
19
    {
20 41
        $this->codeHelper = $codeHelper;
21 41
    }
22
23
    /**
24
     * @param string $fieldFqn
25
     * @param string $entityFqn
26
     *
27
     * @throws DoctrineStaticMetaException
28
     * @SuppressWarnings(PHPMD.StaticAccess)
29
     */
30 67
    public function setEntityHasField(string $entityFqn, string $fieldFqn): void
31
    {
32
        try {
33 67
            $entityReflection          = new \ReflectionClass($entityFqn);
34 67
            $entity                    = PhpClass::fromFile($entityReflection->getFileName());
35 67
            $entityInterfaceFqn        = \str_replace(
36 67
                '\\Entities\\',
37 67
                '\\Entity\\Interfaces\\',
38 67
                $entityFqn
39 67
            ).'Interface';
40 67
            $entityInterfaceReflection = new \ReflectionClass($entityInterfaceFqn);
41 67
            $entityInterface           = PhpInterface::fromFile($entityInterfaceReflection->getFileName());
42 67
            $fieldReflection           = new \ReflectionClass($fieldFqn);
43 67
            $field                     = PhpTrait::fromFile($fieldReflection->getFileName());
44 67
            $fieldInterfaceFqn         = \str_replace(
45 67
                ['Traits', 'Trait'],
46 67
                ['Interfaces', 'Interface'],
47 67
                $fieldFqn
48
            );
49 67
            $fieldInterfaceReflection  = new \ReflectionClass($fieldInterfaceFqn);
50 67
            $this->checkInterfaceLooksLikeField($fieldInterfaceReflection);
51 67
            $fieldInterface = PhpInterface::fromFile($fieldInterfaceReflection->getFileName());
52
        } catch (\Exception $e) {
53
            throw new DoctrineStaticMetaException(
54
                'Failed loading the entity or field from FQN: '.$e->getMessage(),
55
                $e->getCode(),
56
                $e
57
            );
58
        }
59 67
        $entity->addTrait($field);
60 67
        $this->codeHelper->generate($entity, $entityReflection->getFileName());
61 67
        $entityInterface->addInterface($fieldInterface);
62 67
        $this->codeHelper->generate($entityInterface, $entityInterfaceReflection->getFileName());
63 67
    }
64
65
    /**
66
     * @param \ReflectionClass $fieldInterfaceReflection
67
     */
68 67
    protected function checkInterfaceLooksLikeField(\ReflectionClass $fieldInterfaceReflection): void
69
    {
70
        $notFound = [
71 67
            'PROP_',
72
            'DEFAULT_',
73
        ];
74 67
        $consts   = $fieldInterfaceReflection->getConstants();
75 67
        foreach (\array_keys($consts) as $name) {
76 67
            foreach ($notFound as $key => $prefix) {
77 67
                if (0 === strpos($name, $prefix)) {
78 67
                    unset($notFound[$key]);
79
                }
80
            }
81
        }
82 67
        if ([] !== $notFound) {
83
            throw new \InvalidArgumentException(
84
                'Field '.$fieldInterfaceReflection->getName()
85
                .' does not look like a field interface, failed to find the following const prefixes: '
86
                ."\n".print_r($notFound, true)
87
            );
88
        }
89 67
    }
90
}
91