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

updateFakerProviderArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 26
rs 9.568
c 0
b 0
f 0
ccs 22
cts 22
cp 1
cc 2
nc 2
nop 3
crap 2
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\NamespaceHelper;
7
use gossi\codegen\model\PhpClass;
8
use gossi\codegen\model\PhpConstant;
9
10
/**
11
 * Class AbstractTestFakerDataProviderUpdater
12
 *
13
 * @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field
14
 * @SuppressWarnings(PHPMD.StaticAccess)
15
 */
16
class AbstractTestFakerDataProviderUpdater
17
{
18
    /**
19
     * @var NamespaceHelper
20
     */
21
    private $namespaceHelper;
22
    /**
23
     * @var CodeHelper
24
     */
25
    private $codeHelper;
26
    /**
27
     * @var string
28
     */
29
    private $fieldFqn;
30
    /**
31
     * @var string
32
     */
33
    private $entityFqn;
34
35
    /**
36
     * @var string
37
     */
38
    private $projectRootPath;
39
    /**
40
     * @var string
41
     */
42
    private $fakerFqn;
43
    /**
44
     * @var string
45
     */
46
    private $interfaceFqn;
47
    /**
48
     * @var string
49
     */
50
    private $abstractTestPath;
51
    /**
52
     * @var string
53
     */
54
    private $newPropertyConst;
55
56 83
    public function __construct(NamespaceHelper $namespaceHelper, CodeHelper $codeHelper)
57
    {
58 83
        $this->namespaceHelper = $namespaceHelper;
59 83
        $this->codeHelper      = $codeHelper;
60 83
    }
61
62 33
    public function updateFakerProviderArray(string $projectRootPath, string $fieldFqn, string $entityFqn)
63
    {
64 33
        $this->projectRootPath  = $projectRootPath;
65 33
        $this->fieldFqn         = $fieldFqn;
66 33
        $fieldFqnBase           = \str_replace('FieldTrait', '', $this->fieldFqn);
67 33
        $this->entityFqn        = $entityFqn;
68 33
        $this->fakerFqn         = $this->namespaceHelper->tidy(
69 33
            \str_replace('\\Traits\\', '\\FakerData\\', $fieldFqnBase)
70 33
        ) . 'FakerData';
71 33
        $this->interfaceFqn     = $this->namespaceHelper->tidy(
72 33
            \str_replace(
73 33
                '\\Traits\\',
74 33
                '\\Interfaces\\',
75 33
                $fieldFqnBase
76 33
            ) . 'FieldInterface'
77
        );
78 33
        $this->abstractTestPath = $this->projectRootPath . '/tests/Entities/AbstractEntityTest.php';
79 33
        $test                   = PhpClass::fromFile($this->abstractTestPath);
80 33
        $this->newPropertyConst = 'PROP_' . $this->codeHelper->consty($this->namespaceHelper->basename($fieldFqnBase));
81
        try {
82 33
            $constant = $this->updateExisting($test);
83 33
        } catch (\InvalidArgumentException $e) {
84 33
            $constant = $this->createNew();
85
        }
86 33
        $test->setConstant($constant);
87 33
        $this->codeHelper->generate($test, $this->abstractTestPath);
88 33
    }
89
90
    /**
91
     * Get the line that we are going to add to the array
92
     *
93
     * @return string
94
     */
95 33
    private function getLine(): string
96
    {
97 33
        return "\n'$this->entityFqn-'.\\$this->interfaceFqn::$this->newPropertyConst => \\$this->fakerFqn::class\n";
98
    }
99
100 33
    private function updateExisting(PhpClass $test): PhpConstant
101
    {
102 33
        $constant = $test->getConstant('FAKER_DATA_PROVIDERS');
103 5
        $test->removeConstant($constant);
104 5
        $expression = $constant->getExpression();
105 5
        $expression = \str_replace(
106 5
            ']',
107 5
            ",{$this->getLine()}]",
108 5
            $expression
109
        );
110 5
        $constant->setExpression($expression);
111
112 5
        return $constant;
113
    }
114
115 33
    private function createNew(): PhpConstant
116
    {
117 33
        return new PhpConstant(
118 33
            'FAKER_DATA_PROVIDERS',
119 33
            "[\n{$this->getLine()}]",
120 33
            true
121
        );
122
    }
123
}
124