Psr4Classes   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 18 4
A classList() 0 36 5
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope;
4
5
use Illuminate\Support\Str;
6
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
7
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath;
8
9
class Psr4Classes
10
{
11
    /**
12
     * @var array
13
     */
14
    public static $allNamespaces = [];
15
16
    public static $checkedFilesNum = 0;
17
18
    public static function check($checks)
19
    {
20
        $psr4 = ComposerJson::readAutoload();
21
22
        foreach ($psr4 as $psr4Namespace => $psr4Path) {
23
            $files = FilePath::getAllPhpFiles($psr4Path);
24
            foreach ($files as $classFilePath) {
25
                self::$checkedFilesNum++;
26
                $absFilePath = $classFilePath->getRealPath();
27
28
                $tokens = token_get_all(file_get_contents($absFilePath));
29
30
                foreach ($checks as $check) {
31
                    $check::check($tokens, $absFilePath, $classFilePath, $psr4Path, $psr4Namespace);
32
                }
33
            }
34
        }
35
    }
36
37
    public static function classList()
38
    {
39
        $psr4 = ComposerJson::readAutoload();
40
41
        if (self::$allNamespaces) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::$allNamespaces of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
            return self::$allNamespaces;
43
        }
44
45
        foreach ($psr4 as $psr4Namespace => $psr4Path) {
46
            $files = FilePath::getAllPhpFiles($psr4Path);
47
            foreach ($files as $classFilePath) {
48
                $fileName = $classFilePath->getFilename();
49
                if (Str::endsWith($fileName, ['.blade.php'])) {
50
                    continue;
51
                }
52
53
                $filePath = \str_replace(base_path(), '', $classFilePath->getRealPath());
54
55
                $composerPath = \str_replace('/', '\\', $psr4Path);
56
57
                // replace composer base_path with composer namespace
58
                /**
59
                 *  "psr-4": {
60
                 *      "App\\": "app/"
61
                 *  }.
62
                 */
63
                $ns = Str::replaceFirst(\trim($composerPath, '\\'), \trim($psr4Namespace, '\\/'), $filePath);
64
                $t = \str_replace('.php', '', [$ns, $fileName]);
65
                $t = \str_replace('/', '\\', $t); // for linux environments.
66
67
                self::$allNamespaces[$t[1]][] = \trim($t[0], '\\');
68
            }
69
        }
70
71
        return self::$allNamespaces;
72
    }
73
}
74