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 (#93)
by joseph
17:38 queued 30s
created

EntityFieldSetter::checkInterfaceLooksLikeField()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.7283

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 20
rs 9.4888
c 0
b 0
f 0
ccs 9
cts 13
cp 0.6923
cc 5
nc 8
nop 1
crap 5.7283
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 83
    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 83
        parent::__construct(
42 83
            $filesystem,
43 83
            $fileCreationTransaction,
44 83
            $namespaceHelper,
45 83
            $config,
46 83
            $codeHelper,
47 83
            $pathHelper,
48 83
            $findAndReplaceHelper
49
        );
50 83
        $this->updater = $updater;
51 83
    }
52
53
54
    /**
55
     * @param string $fieldFqn
56
     * @param string $entityFqn
57
     *
58
     * @throws DoctrineStaticMetaException
59
     * @SuppressWarnings(PHPMD.StaticAccess)
60
     */
61 70
    public function setEntityHasField(string $entityFqn, string $fieldFqn): void
62
    {
63
        try {
64 70
            $entityReflection          = new \ts\Reflection\ReflectionClass($entityFqn);
65 70
            $entity                    = PhpClass::fromFile($entityReflection->getFileName());
66 70
            $entityInterfaceFqn        = $this->namespaceHelper->getEntityInterfaceFromEntityFqn($entityFqn);
67 70
            $entityInterfaceReflection = new \ts\Reflection\ReflectionClass($entityInterfaceFqn);
68 70
            $entityInterface           = PhpInterface::fromFile($entityInterfaceReflection->getFileName());
69 70
            $fieldReflection           = new \ts\Reflection\ReflectionClass($fieldFqn);
70 70
            $field                     = PhpTrait::fromFile($fieldReflection->getFileName());
71 70
            $fieldInterfaceFqn         = \str_replace(
72 70
                ['Traits', 'Trait'],
73 70
                ['Interfaces', 'Interface'],
74 70
                $fieldFqn
75
            );
76 70
            $fieldInterfaceReflection  = new \ts\Reflection\ReflectionClass($fieldInterfaceFqn);
77 70
            $this->checkInterfaceLooksLikeField($fieldInterfaceReflection);
78 70
            $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 70
        if ($this->alreadyUsingFieldWithThisShortName($entity, $field)) {
87 1
            throw new \InvalidArgumentException('Entity already has a field with this short name');
88
        }
89 70
        $entity->addTrait($field);
90 70
        $this->codeHelper->generate($entity, $entityReflection->getFileName());
91 70
        $entityInterface->addInterface($fieldInterface);
92 70
        $this->codeHelper->generate($entityInterface, $entityInterfaceReflection->getFileName());
93 70
        if ($this->fieldHasFakerProvider($fieldReflection)) {
94 33
            $this->updater->updateFakerProviderArray($this->pathToProjectRoot, $fieldFqn, $entityFqn);
95
        }
96 70
    }
97
98 70
    protected function alreadyUsingFieldWithThisShortName(PhpClass $entity, PhpTrait $field): bool
99
    {
100 70
        $useStatements = $entity->getUseStatements();
101
102 70
        return null !== $useStatements->get($field->getName());
103
    }
104
105 70
    protected function fieldHasFakerProvider(\ts\Reflection\ReflectionClass $fieldReflection): bool
106
    {
107 70
        return \class_exists(
108 70
            $this->namespaceHelper->getFakerProviderFqnFromFieldTraitReflection($fieldReflection)
109
        );
110
    }
111
112
    /**
113
     * @param \ts\Reflection\ReflectionClass $fieldInterfaceReflection
114
     */
115 70
    protected function checkInterfaceLooksLikeField(\ts\Reflection\ReflectionClass $fieldInterfaceReflection): void
116
    {
117
        $lookFor = [
118 70
            'PROP_',
119
            'DEFAULT_',
120
        ];
121 70
        $found   = [];
122 70
        $consts  = $fieldInterfaceReflection->getConstants();
123 70
        foreach (\array_keys($consts) as $name) {
124 70
            foreach ($lookFor as $key => $prefix) {
125 70
                if (\ts\stringStartsWith($name, $prefix)) {
126 70
                    $found[$key] = $prefix;
127
                }
128
            }
129
        }
130 70
        if ($found !== $lookFor) {
131
            throw new \InvalidArgumentException(
132
                'Field ' . $fieldInterfaceReflection->getName()
133
                . ' does not look like a field interface, failed to find the following const prefixes: '
134
                . "\n" . print_r($lookFor, true)
135
            );
136
        }
137 70
    }
138
}
139