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
Push — master ( 51c99d...a4bf8a )
by joseph
24s queued 11s
created

StandardLibraryTestGenerator::createTestForField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
/**
3
 * When working on the standard library fields, this will generate a skeleton test for any fields you generate
4
 */
5
6
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field;
7
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper;
9
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PathHelper;
10
11
class StandardLibraryTestGenerator
12
{
13
    private const FIELDS_PATH = __DIR__.'/../../../Entity/Fields/Traits';
14
15
    private const TESTS_PATH = __DIR__.'/../../../../tests/functional/Entity/Fields/Traits';
16
17
    private const FIELDS_FQN_BASE = 'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\';
18
19
    private const TEST_TEMPLATE = <<<PHP
20
<?php declare(strict_types=1);
21
22
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\FOLDER;
23
24
use EdmondsCommerce\DoctrineStaticMeta\AbstractIntegrationTest;
25
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\AbstractFieldTraitFunctionalTest;
26
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\FOLDER\__CLASSY__FieldInterface;
27
28
class __CLASSY__FieldTraitTest extends AbstractFieldTraitFunctionalTest
29
{
30
    public const WORK_DIR = AbstractIntegrationTest::VAR_PATH.'/'.self::TEST_TYPE.'/__CLASSY__FieldTraitTest/';
31
    protected const TEST_FIELD_FQN =   __CLASSY__FieldTrait::class;
32
    protected const TEST_FIELD_PROP =  __CLASSY__FieldInterface::PROP___CONSTY__;
33
    protected const TEST_FIELD_DEFAULT = __CLASSY__FieldInterface::DEFAULT___CONSTY__;
34
}
35
36
PHP;
37
    /**
38
     * @var CodeHelper
39
     */
40
    protected $codeHelper;
41
    /**
42
     * @var PathHelper
43
     */
44
    protected $pathHelper;
45
46 1
    public function __construct(
47
        CodeHelper $codeHelper,
48
        PathHelper $pathHelper
49
    ) {
50 1
        $this->codeHelper = $codeHelper;
51 1
        $this->pathHelper = $pathHelper;
52 1
    }
53
54
    /**
55
     * @param string $path
56
     *
57
     * @return \RecursiveIteratorIterator|\SplFileInfo[]
58
     */
59
    private function getIteratorForPath(string $path): \RecursiveIteratorIterator
60
    {
61
        $path = \realpath($path);
62
        if (false === $path) {
63
            throw new \InvalidArgumentException($path.' does not exist');
64
        }
65
66
        return new \RecursiveIteratorIterator(
67
            new \RecursiveDirectoryIterator(
68
                $path,
69
                \RecursiveDirectoryIterator::SKIP_DOTS
70
            ),
71
            \RecursiveIteratorIterator::SELF_FIRST
72
        );
73
    }
74
75
    public function assertTestExistsForField(\ReflectionClass $fieldReflection): void
76
    {
77
        $fieldFqn = $fieldReflection->getName();
78
        $testFqn  = $fieldFqn.'Test';
79
        try {
80
            new \ReflectionClass($testFqn);
81
        } catch (\ReflectionException $e) {
82
            $this->createTestForField($fieldReflection);
83
        }
84
    }
85
86
    private function createTestForField(\ReflectionClass $fieldReflection): void
87
    {
88
        $fieldName   = str_replace('FieldTrait', '', $fieldReflection->getShortName());
89
        $contents    = \str_replace(
90
            [
91
                'FOLDER',
92
                '__CLASSY__',
93
                '__CONSTY__',
94
            ],
95
            [
96
                $this->getFolder($fieldReflection),
97
                $this->codeHelper->classy($fieldName),
98
                $this->codeHelper->consty($fieldName),
99
            ],
100
            self::TEST_TEMPLATE
101
        );
102
        $pathForTest = $this->getPathForTest($fieldReflection);
103
        $this->pathHelper->ensurePathExists(\dirname($pathForTest));
104
        \file_put_contents($pathForTest, $contents);
105
    }
106
107
    private function getPathForTest(\ReflectionClass $fieldReflection): string
108
    {
109
        return self::TESTS_PATH.'/'.$this->getFolder($fieldReflection).'/'.$fieldReflection->getShortName().'Test.php';
110
    }
111
112
    private function getFolder(\ReflectionClass $fieldReflection): string
113
    {
114
        $exp = explode('\\', $fieldReflection->getNamespaceName());
115
116
        return end($exp);
117
    }
118
119
    /**
120
     * @return \Generator|\ReflectionClass[]
121
     * @throws \ReflectionException
122
     */
123
    public function getFields(): \Generator
124
    {
125
        $iterator = $this->getIteratorForPath(self::FIELDS_PATH);
126
        foreach ($iterator as $info) {
127
            if (false === $info->isFile()) {
128
                continue;
129
            }
130
            yield $this->getFieldReflectionFromFileInfo($info);
131
        }
132
    }
133
134
    private function getFieldReflectionFromFileInfo(\SplFileInfo $info): \ReflectionClass
135
    {
136
        $class        = $info->getBasename('.php');
137
        $pathExploded = explode('/', $info->getPath());
138
        $folder       = end($pathExploded);
139
        $fqn          = self::FIELDS_FQN_BASE."$folder\\$class";
140
141
        return new \ReflectionClass($fqn);
142
    }
143
}
144