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
21:10
created

FieldInterfaceCreator::getReplaceForDefaultValue()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 26
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 33
rs 7.6666
ccs 0
cts 24
cp 0
crap 110

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