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 (#214)
by joseph
16:19
created

php$0 ➔ getReplaceForDefaultValue()   B

Complexity

Conditions 10

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
dl 0
loc 33
ccs 0
cts 24
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Interfaces;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\AbstractCreator;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ProcessInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\AbstractFieldCreator;
9
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Traits\FieldTraitCreator;
10
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory;
11
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory;
12
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File;
13
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer;
14
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
15
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\TypeHelper;
16
use EdmondsCommerce\DoctrineStaticMeta\Config;
17
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
18
use InvalidArgumentException;
19
use RuntimeException;
20
21
class FieldInterfaceCreator extends AbstractFieldCreator
22
{
23
    public const TEMPLATE_PATH = self::ROOT_TEMPLATE_PATH .
24
                                 '/src/Entity/Fields/Interfaces/' .
25
                                 self::FIND_NAME . 'FieldInterface.php';
26
27
28
    public const SUFFIX = FieldTraitCreator::INTERFACE_SUFFIX;
29
    /**
30
     * @var TypeHelper
31
     */
32
    private $typeHelper;
33
34
    public function __construct(
35
        FileFactory $fileFactory,
36
        NamespaceHelper $namespaceHelper,
37
        Writer $fileWriter,
38
        Config $config,
39
        FindReplaceFactory $findReplaceFactory,
40
        CodeHelper $codeHelper,
41
        TypeHelper $typeHelper
42
    ) {
43
        parent::__construct($fileFactory, $namespaceHelper, $fileWriter, $config, $findReplaceFactory, $codeHelper);
44
        $this->typeHelper = $typeHelper;
45
    }
46
47
48
    public function setNewObjectFqn(string $newObjectFqn): AbstractCreator
49
    {
50
        $this->validateFqnEndsWithSuffix($newObjectFqn);
51
52
        return parent::setNewObjectFqn($newObjectFqn);
53
    }
54
55
    private function validateFqnEndsWithSuffix(string $newObjectFqn): void
56
    {
57
        if (substr($newObjectFqn, 0 - strlen(self::SUFFIX)) === self::SUFFIX) {
58
            return;
59
        }
60
        throw new InvalidArgumentException('$newObjectFqn must end in ' . self::SUFFIX);
61
    }
62
63
64
    protected function configurePipeline(): void
65
    {
66
        parent::configurePipeline();
67
        $this->registerReplaceDefaultValue();
68
        $this->registerReplacePropertyName();
69
        $this->registerReplaceType();
70
    }
71
72
    private function registerReplaceDefaultValue(): void
73
    {
74
        $process = new class($this->getReplaceForDefaultValue()) implements ProcessInterface
75
        {
76
            private const FIND = "'defaultValue'";
77
            /**
78
             * @var string
79
             */
80
            private $replace;
81
82
            /**
83
             * @param string $replace
84
             */
85
            public function __construct(string $replace)
86
            {
87
                $this->replace = $replace;
88
            }
89
90
            public function run(File\FindReplace $findReplace): void
91
            {
92
                $findReplace->findReplace(self::FIND, $this->replace);
93
            }
94
        };
95
        $this->pipeline->register($process);
96
    }
97
98
    private function getReplaceForDefaultValue(): string
99
    {
100
        $defaultType = $this->typeHelper->getType($this->defaultValue);
101
        switch (true) {
102
            case $defaultType === 'null':
103
                return 'null';
104
            case $this->phpType === 'string':
105
                return "'$this->defaultValue'";
106
            case $this->phpType === 'bool':
107
                return (true === $this->defaultValue) ? 'true' : 'false';
108
                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...
109
            case $this->phpType === 'float':
110
                $replace = (string)$this->defaultValue;
111
                if (false === \ts\stringContains($replace, '.')) {
0 ignored issues
show
introduced by
The condition false === stringContains($replace, '.') is always false.
Loading history...
112
                    $replace .= '.0';
113
                }
114
115
                return $replace;
116
            case $this->phpType === 'int':
117
                return (string)$this->defaultValue;
118
            case $this->phpType === trim(MappingHelper::PHP_TYPE_DATETIME, '\\'):
119
                if ($this->defaultValue !== null) {
120
                    throw new InvalidArgumentException(
121
                        'Invalid default value ' . $this->defaultValue
122
                        . 'Currently we only support null as a default for DateTime'
123
                    );
124
                }
125
126
                return 'null';
127
            default:
128
                throw new RuntimeException(
129
                    'failed to calculate replace based on defaultType ' . $defaultType
130
                    . ' and phpType ' . $this->phpType . ' in ' . __METHOD__
131
                );
132
        }
133
    }
134
}
135