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 (#224)
by joseph
22:25
created

ReflectionHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
crap 1.037
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration;
6
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator;
8
use ReflectionException;
9
use RuntimeException;
10
use ts\Reflection\ReflectionClass;
11
use function array_slice;
12
use function str_replace;
13
14
class ReflectionHelper
15
{
16
17
    /**
18
     * @var NamespaceHelper
19
     */
20
    protected $namespaceHelper;
21
22 3
    public function __construct(NamespaceHelper $namespaceHelper)
23
    {
24 3
        $this->namespaceHelper = $namespaceHelper;
25 3
    }
26
27
    /**
28
     * @param ReflectionClass $fieldTraitReflection
29
     *
30
     * @return string
31
     */
32
    public function getFakerProviderFqnFromFieldTraitReflection(ReflectionClass $fieldTraitReflection): string
33
    {
34
        return str_replace(
35
            [
36
                '\\Traits\\',
37
                'FieldTrait',
38
            ],
39
            [
40
                '\\FakerData\\',
41
                'FakerData',
42
            ],
43
            $fieldTraitReflection->getName()
44
        );
45
    }
46
47
    /**
48
     * Work out the entity namespace root from a single entity reflection object.
49
     *
50
     * @param ReflectionClass $entityReflection
51
     *
52
     * @return string
53
     */
54
    public function getEntityNamespaceRootFromEntityReflection(
55
        ReflectionClass $entityReflection
56
    ): string {
57
        return $this->namespaceHelper->tidy(
58
            $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

58
            /** @scrutinizer ignore-type */ $this->namespaceHelper->getNamespaceRootToDirectoryFromFqn(
Loading history...
59
                $entityReflection->getName(),
60
                AbstractGenerator::ENTITIES_FOLDER_NAME
61
            )
62
        );
63
    }
64
65
    /**
66
     * Find which trait is implementing a method in a class,
67
     * If it is not found in a trait, we return the class itself as it must be there
68
     *
69
     * @param ReflectionClass $reflectionClass
70
     * @param string          $methodName
71
     *
72
     * @return ReflectionClass
73
     * @throws ReflectionException
74
     */
75
    public function getTraitImplementingMethod(ReflectionClass $reflectionClass, string $methodName): ?ReflectionClass
76
    {
77
        $traitsWithMethod = [];
78
        foreach ($reflectionClass->getTraits() as $trait) {
79
            try {
80
                $trait->getMethod($methodName);
81
                $traitsWithMethod[] = $trait;
82
            } catch (ReflectionException $e) {
83
                continue;
84
            }
85
        }
86
        if (count($traitsWithMethod) > 1) {
87
            throw new RuntimeException(
88
                'Found more than one trait implementing the method ' . $methodName . ' in ' .
89
                $reflectionClass->getShortName()
90
            );
91
        }
92
        if ([] === $traitsWithMethod) {
93
            return null;
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 1
    public function getTraitProvidingProperty(ReflectionClass $class, string $propertyName): ReflectionClass
109
    {
110 1
        $traitsWithProperty = [];
111 1
        foreach ($class->getTraits() as $trait) {
112
            try {
113 1
                $trait->getProperty($propertyName);
114 1
                $traitsWithProperty[] = $trait;
115 1
            } catch (ReflectionException $e) {
116 1
                continue;
117
            }
118
        }
119 1
        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 1
        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 1
        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 1
    public function getMethodBody(string $methodName, ReflectionClass $reflectionClass): string
147
    {
148 1
        $method      = $reflectionClass->getMethod($methodName);
149 1
        $startLine   = $method->getStartLine() - 1;
150 1
        $length      = $method->getEndLine() - $startLine;
151 1
        $lines       = file($reflectionClass->getFileName());
152 1
        $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 1
        return implode('', $methodLines);
155
    }
156
157
    /**
158
     * @param ReflectionClass $reflectionClass
159
     *
160
     * @return array|string[]
161
     */
162 1
    public function getUseStatements(ReflectionClass $reflectionClass): array
163
    {
164 1
        $content = \ts\file_get_contents($reflectionClass->getFileName());
165 1
        preg_match_all('%^use.+?;%m', $content, $matches);
166
167 1
        return $matches[0];
168
    }
169
}
170