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
14:47
created

EntityFieldSetter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 83.02%

Importance

Changes 0
Metric Value
wmc 12
eloc 51
dl 0
loc 104
rs 10
c 0
b 0
f 0
ccs 44
cts 53
cp 0.8302

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A fieldHasFakerProvider() 0 4 1
A setEntityHasField() 0 34 4
A alreadyUsingFieldWithThisShortName() 0 5 1
A checkInterfaceLooksLikeField() 0 20 5
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($filesystem, $fileCreationTransaction, $namespaceHelper, $config, $codeHelper, $pathHelper,
42 83
                            $findAndReplaceHelper);
43 83
        $this->updater = $updater;
44 83
    }
45
46
47
    /**
48
     * @param string $fieldFqn
49
     * @param string $entityFqn
50
     *
51
     * @throws DoctrineStaticMetaException
52
     * @SuppressWarnings(PHPMD.StaticAccess)
53
     */
54 70
    public function setEntityHasField(string $entityFqn, string $fieldFqn): void
55
    {
56
        try {
57 70
            $entityReflection          = new \ts\Reflection\ReflectionClass($entityFqn);
58 70
            $entity                    = PhpClass::fromFile($entityReflection->getFileName());
59 70
            $entityInterfaceFqn        = $this->namespaceHelper->getEntityInterfaceFromEntityFqn($entityFqn);
60 70
            $entityInterfaceReflection = new \ts\Reflection\ReflectionClass($entityInterfaceFqn);
61 70
            $entityInterface           = PhpInterface::fromFile($entityInterfaceReflection->getFileName());
62 70
            $fieldReflection           = new \ts\Reflection\ReflectionClass($fieldFqn);
63 70
            $field                     = PhpTrait::fromFile($fieldReflection->getFileName());
64 70
            $fieldInterfaceFqn         = \str_replace(
65 70
                ['Traits', 'Trait'],
66 70
                ['Interfaces', 'Interface'],
67 70
                $fieldFqn
68
            );
69 70
            $fieldInterfaceReflection  = new \ts\Reflection\ReflectionClass($fieldInterfaceFqn);
70 70
            $this->checkInterfaceLooksLikeField($fieldInterfaceReflection);
71 70
            $fieldInterface = PhpInterface::fromFile($fieldInterfaceReflection->getFileName());
72
        } catch (\Exception $e) {
73
            throw new DoctrineStaticMetaException(
74
                'Failed loading the entity or field from FQN: ' . $e->getMessage(),
75
                $e->getCode(),
76
                $e
77
            );
78
        }
79 70
        if ($this->alreadyUsingFieldWithThisShortName($entity, $field)) {
80 1
            throw new \InvalidArgumentException('Entity already has a field with this short name');
81
        }
82 70
        $entity->addTrait($field);
83 70
        $this->codeHelper->generate($entity, $entityReflection->getFileName());
84 70
        $entityInterface->addInterface($fieldInterface);
85 70
        $this->codeHelper->generate($entityInterface, $entityInterfaceReflection->getFileName());
86 70
        if ($this->fieldHasFakerProvider($fieldReflection)) {
87 33
            $this->updater->updateFakerProviderArray($this->pathToProjectRoot, $fieldFqn, $entityFqn);
88
        }
89 70
    }
90
91 70
    protected function alreadyUsingFieldWithThisShortName(PhpClass $entity, PhpTrait $field): bool
92
    {
93 70
        $useStatements = $entity->getUseStatements();
94
95 70
        return null !== $useStatements->get($field->getName());
96
    }
97
98 70
    protected function fieldHasFakerProvider(\ts\Reflection\ReflectionClass $fieldReflection): bool
99
    {
100 70
        return \class_exists(
101 70
            $this->namespaceHelper->getFakerProviderFqnFromFieldTraitReflection($fieldReflection)
102
        );
103
    }
104
105
    /**
106
     * @param \ts\Reflection\ReflectionClass $fieldInterfaceReflection
107
     */
108 70
    protected function checkInterfaceLooksLikeField(\ts\Reflection\ReflectionClass $fieldInterfaceReflection): void
109
    {
110
        $lookFor = [
111 70
            'PROP_',
112
            'DEFAULT_',
113
        ];
114 70
        $found   = [];
115 70
        $consts  = $fieldInterfaceReflection->getConstants();
116 70
        foreach (\array_keys($consts) as $name) {
117 70
            foreach ($lookFor as $key => $prefix) {
118 70
                if (\ts\stringStartsWith($name, $prefix)) {
119 70
                    $found[$key] = $prefix;
120
                }
121
            }
122
        }
123 70
        if ($found !== $lookFor) {
124
            throw new \InvalidArgumentException(
125
                'Field ' . $fieldInterfaceReflection->getName()
126
                . ' does not look like a field interface, failed to find the following const prefixes: '
127
                . "\n" . print_r($lookFor, true)
128
            );
129
        }
130 70
    }
131
}
132