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 (#199)
by joseph
21:04
created

ReflectionHelper::getTraitProvidingProperty()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 20.694

Importance

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

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

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