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.
Passed
Pull Request — master (#214)
by joseph
20:33
created

ReflectionHelper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 13.68%

Importance

Changes 0
Metric Value
eloc 55
c 0
b 0
f 0
dl 0
loc 156
rs 10
ccs 13
cts 95
cp 0.1368
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getUseStatements() 0 6 1
B getTraitProvidingProperty() 0 28 7
A getTraitImplementingMethod() 0 25 5
A getMethodBody() 0 9 1
A getFakerProviderFqnFromFieldTraitReflection() 0 12 1
A getEntityNamespaceRootFromEntityReflection() 0 7 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator;
6
use ReflectionException;
7
use RuntimeException;
8
use ts\Reflection\ReflectionClass;
9
use function array_slice;
10
use function str_replace;
11
12
class ReflectionHelper
13
{
14
15
    /**
16
     * @var NamespaceHelper
17
     */
18
    protected $namespaceHelper;
19
20 2
    public function __construct(NamespaceHelper $namespaceHelper)
21
    {
22 2
        $this->namespaceHelper = $namespaceHelper;
23 2
    }
24
25
    /**
26
     * @param ReflectionClass $fieldTraitReflection
27
     *
28
     * @return string
29
     */
30 1
    public function getFakerProviderFqnFromFieldTraitReflection(ReflectionClass $fieldTraitReflection
31
    ): string {
32 1
        return str_replace(
33
            [
34 1
                '\\Traits\\',
35
                'FieldTrait',
36
            ],
37
            [
38 1
                '\\FakerData\\',
39
                'FakerData',
40
            ],
41 1
            $fieldTraitReflection->getName()
42
        );
43
    }
44
45
    /**
46
     * Work out the entity namespace root from a single entity reflection object.
47
     *
48
     * @param ReflectionClass $entityReflection
49
     *
50
     * @return string
51
     */
52 1
    public function getEntityNamespaceRootFromEntityReflection(
53
        ReflectionClass $entityReflection
54
    ): string {
55 1
        return $this->namespaceHelper->tidy(
56 1
            $this->namespaceHelper->getNamespaceRootToDirectoryFromFqn(
0 ignored issues
show
Bug introduced by
It seems like $this->namespaceHelper->...::ENTITIES_FOLDER_NAME) can also be of type null; however, parameter $namespace of EdmondsCommerce\Doctrine...NamespaceHelper::tidy() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            /** @scrutinizer ignore-type */ $this->namespaceHelper->getNamespaceRootToDirectoryFromFqn(
Loading history...
57 1
                $entityReflection->getName(),
58 1
                AbstractGenerator::ENTITIES_FOLDER_NAME
59
            )
60
        );
61
    }
62
63
    /**
64
     * Find which trait is implementing a method in a class
65
     *
66
     * @param ReflectionClass $class
67
     * @param string          $methodName
68
     *
69
     * @return ReflectionClass
70
     * @throws ReflectionException
71
     */
72
    public function getTraitImplementingMethod(ReflectionClass $class, string $methodName): ReflectionClass
73
    {
74
        $traitsWithMethod = [];
75
        foreach ($class->getTraits() as $trait) {
76
            try {
77
                $trait->getMethod($methodName);
78
                $traitsWithMethod[] = $trait;
79
            } catch (ReflectionException $e) {
80
                continue;
81
            }
82
        }
83
        if (count($traitsWithMethod) > 1) {
84
            throw new RuntimeException(
85
                'Found more than one trait implementing the method ' . $methodName . ' in ' .
86
                $class->getShortName()
87
            );
88
        }
89
        if ([] === $traitsWithMethod) {
90
            throw new RuntimeException(
91
                'Failed finding trait implementing the method ' . $methodName . ' in ' .
92
                $class->getShortName()
93
            );
94
        }
95
96
        return current($traitsWithMethod);
97
    }
98
99
    /**
100
     * Find which trait is implementing a method in a class
101
     *
102
     * @param ReflectionClass $class
103
     * @param string          $propertyName
104
     *
105
     * @return ReflectionClass
106
     * @throws ReflectionException
107
     */
108
    public function getTraitProvidingProperty(ReflectionClass $class, string $propertyName): ReflectionClass
109
    {
110
        $traitsWithProperty = [];
111
        foreach ($class->getTraits() as $trait) {
112
            try {
113
                $trait->getProperty($propertyName);
114
                $traitsWithProperty[] = $trait;
115
            } catch (ReflectionException $e) {
116
                continue;
117
            }
118
        }
119
        if ([] === $traitsWithProperty) {
120
            if ($class->isTrait() && $class->hasProperty($propertyName)) {
121
                return $class;
122
            }
123
            throw new RuntimeException(
124
                'Failed finding trait providing the property ' . $propertyName . ' in ' .
125
                $class->getShortName()
126
            );
127
        }
128
        if (count($traitsWithProperty) > 1) {
129
            throw new RuntimeException(
130
                'Found more than one trait providing the property ' . $propertyName . ' in ' .
131
                $class->getShortName()
132
            );
133
        }
134
135
        return current($traitsWithProperty);
136
    }
137
138
    /**
139
     * Get the full method body using reflection
140
     *
141
     * @param string          $methodName
142
     * @param ReflectionClass $reflectionClass
143
     *
144
     * @return string
145
     */
146
    public function getMethodBody(string $methodName, ReflectionClass $reflectionClass): string
147
    {
148
        $method      = $reflectionClass->getMethod($methodName);
149
        $startLine   = $method->getStartLine() - 1;
150
        $length      = $method->getEndLine() - $startLine;
151
        $lines       = file($reflectionClass->getFileName());
152
        $methodLines = array_slice($lines, $startLine, $length);
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
        $methodLines = array_slice(/** @scrutinizer ignore-type */ $lines, $startLine, $length);
Loading history...
153
154
        return implode('', $methodLines);
155
    }
156
157
    /**
158
     * @param ReflectionClass $reflectionClass
159
     *
160
     * @return array|string[]
161
     */
162
    public function getUseStatements(ReflectionClass $reflectionClass): array
163
    {
164
        $content = \ts\file_get_contents($reflectionClass->getFileName());
165
        preg_match_all('%^use.+?;%m', $content, $matches);
166
167
        return $matches[0];
168
    }
169
}
170