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 ( 645b45...12d1b3 )
by Cees-Jan
17s
created

functions.php ➔ listInstantiatableClassesInDirectories()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 18
loc 18
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 9.6666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A functions.php$0 ➔ accept() 11 11 2
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus;
4
5
use Roave\BetterReflection\BetterReflection;
6
use Roave\BetterReflection\Reflection\ReflectionClass;
7
use Roave\BetterReflection\Reflector\ClassReflector;
8
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
9
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
10
use Roave\BetterReflection\SourceLocator\Type\AutoloadSourceLocator;
11
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;
12
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
13
14
/**
15
 * get a list of all classes in the given directories.
16
 *
17
 * Based on: https://github.com/Roave/BetterReflection/blob/396a07c9d276cb9ffba581b24b2dadbb542d542e/demo/parsing-whole-directory/example2.php.
18
 *
19
 * @param string[] $directories
20
 *
21
 * @return iterable
22
 */
23 View Code Duplication
function listClassesInDirectories(string ...$directories): iterable
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25 4
    $sourceLocator = new AggregateSourceLocator([
26 4
        new DirectoriesSourceLocator(
27 4
            $directories,
0 ignored issues
show
Documentation introduced by
$directories is of type array<integer,array<integer,string>>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
28 4
            (new BetterReflection())->astLocator()
29
        ),
30
        // ↓ required to autoload parent classes/interface from another directory than /src (e.g. /vendor)
31 4
        new AutoloadSourceLocator((new BetterReflection())->astLocator()),
32
    ]);
33
34 4
    foreach ((new ClassReflector($sourceLocator))->getAllClasses() as $class) {
35 4
        yield $class->getName();
36
    }
37 4
}
38
39
/**
40
 * get a list of all classes in the given directory.
41
 *
42
 * @param string $directory
43
 *
44
 * @return iterable
45
 */
46
function listClassesInDirectory(string $directory): iterable
47
{
48 1
    yield from listClassesInDirectories($directory);
49 1
}
50
51
/**
52
 * get a list of all classes in the given file.
53
 *
54
 * @param string $file
55
 *
56
 * @return iterable
57
 */
58 View Code Duplication
function listClassesInFile(string $file): iterable
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
{
60 2
    $sourceLocator = new AggregateSourceLocator([
61 2
        new SingleFileSourceLocator(
62 2
            $file,
63 2
            (new BetterReflection())->astLocator()
64
        ),
65
        // ↓ required to autoload parent classes/interface from another directory (e.g. /vendor)
66 2
        new AutoloadSourceLocator((new BetterReflection())->astLocator()),
67
    ]);
68
69 2
    foreach ((new ClassReflector($sourceLocator))->getAllClasses() as $class) {
70 2
        yield $class->getName();
71
    }
72 2
}
73
74
/**
75
 * get a list of all classes in the given files.
76
 *
77
 * @param string[] $files
78
 *
79
 * @return iterable
80
 */
81
function listClassesInFiles(string ...$files): iterable
82
{
83 1
    foreach ($files as $file) {
84 1
        foreach (listClassesInFile($file) as $class) {
85 1
            yield $class;
86
        }
87
    }
88 1
}
89
90 View Code Duplication
function listInstantiatableClassesInDirectories(string ...$directories): iterable
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
{
92 1
    $iterator = listClassesInDirectories(...$directories);
93
94
    return new class($iterator) extends \FilterIterator {
95
        public function accept(): bool
96
        {
97 1
            $className = $this->getInnerIterator()->current();
98
            try {
99 1
                $reflectionClass = ReflectionClass::createFromName($className);
100
101 1
                return $reflectionClass->isInstantiable();
102
            } catch (IdentifierNotFound $exception) {
103
                return false;
104
            }
105
        }
106
    };
107
}
108
109
function listInstantiatableClassesInDirectory(string $directory): iterable
110
{
111 1
    yield from listInstantiatableClassesInDirectories($directory);
112 1
}
113
114 View Code Duplication
function listNonInstantiatableClassesInDirectories(string ...$directories): iterable
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
{
116 1
    $iterator = listClassesInDirectories(...$directories);
117
118
    return new class($iterator) extends \FilterIterator {
119
        public function accept(): bool
120
        {
121 1
            $className = $this->getInnerIterator()->current();
122
            try {
123 1
                $reflectionClass = ReflectionClass::createFromName($className);
124
125 1
                return $reflectionClass->isInstantiable() === false;
126
            } catch (IdentifierNotFound $exception) {
127
                return true;
128
            }
129
        }
130
    };
131
}
132
133
function listNonInstantiatableClassesInDirectory(string $directory): iterable
134
{
135 1
    yield from listNonInstantiatableClassesInDirectories($directory);
136
}
137