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.

FieldInterfaceCreator::configurePipeline()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Interfaces;
6
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\AbstractCreator;
9
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\FindReplaceProcess;
10
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\AbstractFieldCreator;
11
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Traits\FieldTraitCreator;
12
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory;
13
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory;
14
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer;
15
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
16
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\TypeHelper;
17
use EdmondsCommerce\DoctrineStaticMeta\Config;
18
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
19
use InvalidArgumentException;
20
use RuntimeException;
21
22
/**
23
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24
 */
25
class FieldInterfaceCreator extends AbstractFieldCreator
26
{
27
    public const TEMPLATE_PATH = self::ROOT_TEMPLATE_PATH .
28
                                 '/src/Entity/Fields/Interfaces/' .
29
                                 self::FIND_NAME . 'FieldInterface.php';
30
31
32
    public const SUFFIX = FieldTraitCreator::INTERFACE_SUFFIX;
33
    /**
34
     * @var TypeHelper
35
     */
36
    private $typeHelper;
37
38
    public function __construct(
39
        FileFactory $fileFactory,
40
        NamespaceHelper $namespaceHelper,
41
        Writer $fileWriter,
42
        Config $config,
43
        FindReplaceFactory $findReplaceFactory,
44
        CodeHelper $codeHelper,
45
        TypeHelper $typeHelper
46
    ) {
47
        parent::__construct($fileFactory, $namespaceHelper, $fileWriter, $config, $findReplaceFactory, $codeHelper);
48
        $this->typeHelper = $typeHelper;
49
    }
50
51
52
    public function setNewObjectFqn(string $newObjectFqn): AbstractCreator
53
    {
54
        $this->validateFqnEndsWithSuffix($newObjectFqn);
55
56
        return parent::setNewObjectFqn($newObjectFqn);
57
    }
58
59
    private function validateFqnEndsWithSuffix(string $newObjectFqn): void
60
    {
61
        if (substr($newObjectFqn, 0 - strlen(self::SUFFIX)) === self::SUFFIX) {
62
            return;
63
        }
64
        throw new InvalidArgumentException('$newObjectFqn must end in ' . self::SUFFIX);
65
    }
66
67
68
    protected function configurePipeline(): void
69
    {
70
        parent::configurePipeline();
71
        $this->registerReplaceDefaultValue();
72
        $this->registerReplacePropertyName();
73
        $this->registerReplaceType();
74
    }
75
76
    private function registerReplaceDefaultValue(): void
77
    {
78
        $find    = "'defaultValue'";
79
        $replace = $this->getReplaceForDefaultValue();
80
        $process = new FindReplaceProcess($find, $replace);
81
        $this->pipeline->register($process);
82
    }
83
84
    /**
85
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
86
     * @return string
87
     */
88
    private function getReplaceForDefaultValue(): string
89
    {
90
        $defaultType = $this->typeHelper->getType($this->defaultValue);
91
        switch (true) {
92
            case $defaultType === 'null':
93
                return 'null';
94
            case $this->phpType === 'string':
95
                return "'$this->defaultValue'";
96
            case $this->phpType === 'bool':
97
                return (true === $this->defaultValue) ? 'true' : 'false';
98
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
99
            case $this->phpType === 'float':
100
                $replace = (string)$this->defaultValue;
101
                if (false === \ts\stringContains($replace, '.')) {
0 ignored issues
show
introduced by
The condition false === stringContains($replace, '.') is always false.
Loading history...
102
                    $replace .= '.0';
103
                }
104
105
                return $replace;
106
            case $this->phpType === 'int':
107
                return (string)$this->defaultValue;
108
            case $this->phpType === trim(MappingHelper::PHP_TYPE_DATETIME, '\\'):
109
                if ($this->defaultValue !== null) {
110
                    throw new InvalidArgumentException(
111
                        'Invalid default value ' . $this->defaultValue
112
                        . 'Currently we only support null as a default for DateTime'
113
                    );
114
                }
115
116
                return 'null';
117
            default:
118
                throw new RuntimeException(
119
                    'failed to calculate replace based on defaultType ' . $defaultType
120
                    . ' and phpType ' . $this->phpType . ' in ' . __METHOD__
121
                );
122
        }
123
    }
124
}
125