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
Pull Request — master (#100)
by joseph
18:26
created

EntityFieldSetter::setEntityHasField()   A

Complexity

Conditions 4
Paths 14

Size

Total Lines 38
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 4.0671

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 38
ccs 26
cts 31
cp 0.8387
rs 9.424
c 0
b 0
f 0
cc 4
nc 14
nop 2
crap 4.0671
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\CodeGeneration\Generator\AbstractGenerator;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FileCreationTransaction;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FindAndReplaceHelper;
9
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
10
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PathHelper;
11
use EdmondsCommerce\DoctrineStaticMeta\Config;
12
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
13
use gossi\codegen\model\PhpClass;
14
use gossi\codegen\model\PhpInterface;
15
use gossi\codegen\model\PhpTrait;
16
use Symfony\Component\Filesystem\Filesystem;
17
18
/**
19
 * Class EntityFieldSetter
20
 *
21
 * @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field
22
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
 */
24
class EntityFieldSetter extends AbstractGenerator
25
{
26
    /**
27
     * @var AbstractTestFakerDataProviderUpdater
28
     */
29
    protected $updater;
30
31 81
    public function __construct(
32
        Filesystem $filesystem,
33
        FileCreationTransaction $fileCreationTransaction,
34
        NamespaceHelper $namespaceHelper,
35
        Config $config,
36
        CodeHelper $codeHelper,
37
        PathHelper $pathHelper,
38
        FindAndReplaceHelper $findAndReplaceHelper,
39
        AbstractTestFakerDataProviderUpdater $updater
40
    ) {
41 81
        parent::__construct(
42 81
            $filesystem,
43 81
            $fileCreationTransaction,
44 81
            $namespaceHelper,
45 81
            $config,
46 81
            $codeHelper,
47 81
            $pathHelper,
48 81
            $findAndReplaceHelper
49
        );
50 81
        $this->updater = $updater;
51 81
    }
52
53
54
    /**
55
     * @param string $fieldFqn
56
     * @param string $entityFqn
57
     *
58
     * @throws DoctrineStaticMetaException
59
     * @SuppressWarnings(PHPMD.StaticAccess)
60
     */
61 68
    public function setEntityHasField(string $entityFqn, string $fieldFqn): void
62
    {
63
        try {
64 68
            $entityReflection          = new \ts\Reflection\ReflectionClass($entityFqn);
65 68
            $entity                    = PhpClass::fromFile($entityReflection->getFileName());
66 68
            $entityInterfaceFqn        = $this->namespaceHelper->getEntityInterfaceFromEntityFqn($entityFqn);
67 68
            $entityInterfaceReflection = new \ts\Reflection\ReflectionClass($entityInterfaceFqn);
68 68
            $entityInterface           = PhpInterface::fromFile($entityInterfaceReflection->getFileName());
69 68
            $fieldReflection           = new \ts\Reflection\ReflectionClass($fieldFqn);
70 68
            $field                     = PhpTrait::fromFile($fieldReflection->getFileName());
71 68
            $fieldInterfaceFqn         = \str_replace(
72 68
                ['Traits', 'Trait'],
73 68
                ['Interfaces', 'Interface'],
74 68
                $fieldFqn
75
            );
76 68
            $fieldInterfaceReflection  = new \ts\Reflection\ReflectionClass($fieldInterfaceFqn);
77 68
            $this->checkInterfaceLooksLikeField($fieldInterfaceReflection);
78 68
            $fieldInterface = PhpInterface::fromFile($fieldInterfaceReflection->getFileName());
79
        } catch (\Exception $e) {
80
            throw new DoctrineStaticMetaException(
81
                'Failed loading the entity or field from FQN: ' . $e->getMessage(),
82
                $e->getCode(),
83
                $e
84
            );
85
        }
86 68
        if ($this->alreadyUsingFieldWithThisShortName($entity, $field)) {
87 1
            throw new \InvalidArgumentException(
88 1
                'Entity already has a field with the the short name: ' . $field->getName()
89 1
                . "\n\nUse statements:" . print_r($entity->getUseStatements(), true)
90 1
                . "\n\nProperty names:" . print_r($entity->getPropertyNames(), true)
91
            );
92
        }
93 68
        $entity->addTrait($field);
94 68
        $this->codeHelper->generate($entity, $entityReflection->getFileName());
95 68
        $entityInterface->addInterface($fieldInterface);
96 68
        $this->codeHelper->generate($entityInterface, $entityInterfaceReflection->getFileName());
97 68
        if ($this->fieldHasFakerProvider($fieldReflection)) {
98 31
            $this->updater->updateFakerProviderArray($this->pathToProjectRoot, $fieldFqn, $entityFqn);
99
        }
100 68
    }
101
102 68
    protected function alreadyUsingFieldWithThisShortName(PhpClass $entity, PhpTrait $field): bool
103
    {
104 68
        $useStatements = $entity->getUseStatements();
105
106 68
        return null !== $useStatements->get($field->getName());
107
    }
108
109 68
    protected function fieldHasFakerProvider(\ts\Reflection\ReflectionClass $fieldReflection): bool
110
    {
111 68
        return \class_exists(
112 68
            $this->namespaceHelper->getFakerProviderFqnFromFieldTraitReflection($fieldReflection)
113
        );
114
    }
115
116
    /**
117
     * @param \ts\Reflection\ReflectionClass $fieldInterfaceReflection
118
     */
119 68
    protected function checkInterfaceLooksLikeField(\ts\Reflection\ReflectionClass $fieldInterfaceReflection): void
120
    {
121
        $lookFor = [
122 68
            'PROP_',
123
            'DEFAULT_',
124
        ];
125 68
        $found   = [];
126 68
        $consts  = $fieldInterfaceReflection->getConstants();
127 68
        foreach (\array_keys($consts) as $name) {
128 68
            foreach ($lookFor as $key => $prefix) {
129 68
                if (\ts\stringStartsWith($name, $prefix)) {
130 68
                    $found[$key] = $prefix;
131
                }
132
            }
133
        }
134 68
        if ($found !== $lookFor) {
135
            throw new \InvalidArgumentException(
136
                'Field ' . $fieldInterfaceReflection->getName()
137
                . ' does not look like a field interface, failed to find the following const prefixes: '
138
                . "\n" . print_r($lookFor, true)
139
            );
140
        }
141 68
    }
142
}
143