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 ( 59446a...77a017 )
by Constantin
02:32
created

ClassDiscovery   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 8.47 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.59%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 10
loc 118
ccs 35
cts 37
cp 0.9459
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A discoverListeners() 0 18 3
A isListenerFileName() 0 4 1
A extractClassFromFileIfAccepted() 0 10 2
A readFile() 0 4 1
A getDiscoveredClasses() 0 4 1
A filterFiles() 0 6 1
A getClassIfAccepted() 0 10 2
A sort() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\CodeAnalysis;
7
8
9
use Gica\CodeAnalysis\MethodListenerDiscovery\ClassSorter;
10
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator;
11
use Gica\CodeAnalysis\Traits\FilesInDirectoryExtracter;
12
13
class ClassDiscovery
14
{
15
    use FilesInDirectoryExtracter;
16
17
    protected $discoveredClasses = [];
18
19
    /** @var ListenerClassValidator */
20
    private $classValidator;
21
    /**
22
     * @var ClassSorter
23
     */
24
    private $classSorter;
25
    /**
26
     * @var PhpClassInFileInspector
27
     */
28
    private $phpClassInFileInspector;
29
30 1 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
31
        ListenerClassValidator $classValidator,
32
        ClassSorter $classSorter,
33
        PhpClassInFileInspector $phpClassInFileInspector = null
34
    )
35
    {
36 1
        $this->classValidator = $classValidator;
37 1
        $this->classSorter = $classSorter;
38 1
        $this->phpClassInFileInspector = $phpClassInFileInspector ?? new PhpClassInFileInspector;
39 1
    }
40
41
42 1
    public function discoverListeners($directory)
43
    {
44 1
        $files = $this->getFilesInDirectory($directory);
45
46 1
        $files = $this->filterFiles($files);
47
48 1
        foreach ($files as $file) {
49 1
            $fullFilePath = $file;
50
51 1
            $extractedClass = $this->extractClassFromFileIfAccepted($fullFilePath);
52
53 1
            if ($extractedClass) {
54 1
                $this->discoveredClasses[] = $extractedClass;
55
            }
56
        }
57
58 1
        $this->discoveredClasses = $this->sort($this->discoveredClasses);
59 1
    }
60
61
    /**
62
     * @param string $filePath
63
     * @return bool
64
     */
65 1
    protected function isListenerFileName($filePath)
66
    {
67 1
        return preg_match('#\.php$#ims', $filePath);
68
    }
69
70
    /**
71
     * @param $fullFilePath
72
     * @return bool|\ReflectionClass
73
     */
74 1
    protected function extractClassFromFileIfAccepted($fullFilePath)
75
    {
76 1
        $fqn = $this->phpClassInFileInspector->getFullyQualifiedClassName($fullFilePath);
77
78 1
        if (!$fqn) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fqn of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
79 1
            return false;
80
        }
81
82 1
        return $this->getClassIfAccepted($fqn);
83
    }
84
85
    protected function readFile($fullFilePath)
86
    {
87
        return file_get_contents($fullFilePath);
88
    }
89
90
    /**
91
     * @return \ReflectionClass[]
92
     */
93 1
    public function getDiscoveredClasses()
94
    {
95 1
        return $this->discoveredClasses;
96
    }
97
98
    protected function filterFiles(array $files)
99
    {
100 1
        return array_filter($files, function ($file) {
101 1
            return $this->isListenerFileName($file);
102 1
        });
103
    }
104
105
    /**
106
     * @param $className
107
     * @return \ReflectionClass|null
108
     */
109 1
    private function getClassIfAccepted($className)
110
    {
111 1
        $reflectionClass = new \ReflectionClass($className);
112
113 1
        if ($this->classValidator->isClassAccepted($reflectionClass)) {
114 1
            return $reflectionClass;
115
        }
116
117 1
        return null;
118
    }
119
120
    /**
121
     * @param \ReflectionClass[] $discoveredClasses
122
     * @return \ReflectionClass[]
123
     */
124 1
    private function sort($discoveredClasses)
125
    {
126 1
        usort($discoveredClasses, $this->classSorter);
127
128 1
        return $discoveredClasses;
129
    }
130
}