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 ( 2a6b46...0d82f1 )
by joseph
20s queued 14s
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
12
use function array_slice;
13
use function str_replace;
14
15
class ReflectionHelper
16
{
17
18
    /**
19
     * @var NamespaceHelper
20
     */
21
    protected $namespaceHelper;
22
23 5
    public function __construct(NamespaceHelper $namespaceHelper)
24
    {
25 5
        $this->namespaceHelper = $namespaceHelper;
26 5
    }
27
28
    /**
29
     * @param ReflectionClass $fieldTraitReflection
30
     *
31
     * @return string
32
     */
33 1
    public function getFakerProviderFqnFromFieldTraitReflection(ReflectionClass $fieldTraitReflection): string
34
    {
35 1
        return str_replace(
36
            [
37 1
                '\\Traits\\',
38
                'FieldTrait',
39
            ],
40
            [
41 1
                '\\FakerData\\',
42
                'FakerData',
43
            ],
44 1
            $fieldTraitReflection->getName()
45
        );
46
    }
47
48
    /**
49
     * Work out the entity namespace root from a single entity reflection object.
50
     *
51
     * @param ReflectionClass $entityReflection
52
     *
53
     * @return string
54
     */
55 1
    public function getEntityNamespaceRootFromEntityReflection(
56
        ReflectionClass $entityReflection
57
    ): string {
58 1
        return $this->namespaceHelper->tidy(
59 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

59
            /** @scrutinizer ignore-type */ $this->namespaceHelper->getNamespaceRootToDirectoryFromFqn(
Loading history...
60 1
                $entityReflection->getName(),
61 1
                AbstractGenerator::ENTITIES_FOLDER_NAME
62
            )
63
        );
64
    }
65
66
    /**
67
     * Find which trait is implementing a method in a class
68
     *
69
     * @param ReflectionClass $class
70
     * @param string          $methodName
71
     *
72
     * @return ReflectionClass
73
     * @throws ReflectionException
74
     */
75
    public function getTraitImplementingMethod(ReflectionClass $class, string $methodName): ReflectionClass
76
    {
77
        $traitsWithMethod = [];
78
        foreach ($class->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
                $class->getShortName()
90
            );
91
        }
92
        if ([] === $traitsWithMethod) {
93
            throw new RuntimeException(
94
                'Failed finding trait implementing the method ' . $methodName . ' in ' .
95
                $class->getShortName()
96
            );
97
        }
98
99
        return current($traitsWithMethod);
100
    }
101
102
    /**
103
     * Find which trait is implementing a method in a class
104
     *
105
     * @param ReflectionClass $class
106
     * @param string          $propertyName
107
     *
108
     * @return ReflectionClass
109
     * @throws ReflectionException
110
     */
111 1
    public function getTraitProvidingProperty(ReflectionClass $class, string $propertyName): ReflectionClass
112
    {
113 1
        $traitsWithProperty = [];
114 1
        foreach ($class->getTraits() as $trait) {
115
            try {
116 1
                $trait->getProperty($propertyName);
117 1
                $traitsWithProperty[] = $trait;
118 1
            } catch (ReflectionException $e) {
119 1
                continue;
120
            }
121
        }
122 1
        if ([] === $traitsWithProperty) {
123
            if ($class->isTrait() && $class->hasProperty($propertyName)) {
124
                return $class;
125
            }
126
            throw new RuntimeException(
127
                'Failed finding trait providing the property ' . $propertyName . ' in ' .
128
                $class->getShortName()
129
            );
130
        }
131 1
        if (count($traitsWithProperty) > 1) {
132
            throw new RuntimeException(
133
                'Found more than one trait providing the property ' . $propertyName . ' in ' .
134
                $class->getShortName()
135
            );
136
        }
137
138 1
        return current($traitsWithProperty);
139
    }
140
141
    /**
142
     * Get the full method body using reflection
143
     *
144
     * @param string          $methodName
145
     * @param ReflectionClass $reflectionClass
146
     *
147
     * @return string
148
     */
149 1
    public function getMethodBody(string $methodName, ReflectionClass $reflectionClass): string
150
    {
151 1
        $method      = $reflectionClass->getMethod($methodName);
152 1
        $startLine   = $method->getStartLine() - 1;
153 1
        $length      = $method->getEndLine() - $startLine;
154 1
        $lines       = file($reflectionClass->getFileName());
155 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

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