Completed
Push — master ( 31e550...936008 )
by Maarten
01:19
created

ModelMapper::findClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Services;
4
5
use RecursiveDirectoryIterator;
6
use RecursiveIteratorIterator;
7
use PhpParser\Node\Stmt\Class_;
8
use PhpParser\Node\Stmt\Namespace_;
9
use PhpParser\NodeTraverser;
10
use PhpParser\NodeVisitor\NameResolver;
11
use PhpParser\ParserFactory;
12
13
class ModelMapper
14
{
15
    public static $models = [];
16
17
    /**
18
     * Maps subclasses of Illuminate\Database\Eloquent\Model
19
     * found in app_path()
20
     *
21
     * @return array
22
     * @throws \ReflectionException
23
     */
24
    public static function map(): array
25
    {
26
        $files = new RecursiveIteratorIterator(
27
            new RecursiveDirectoryIterator(app_path()), RecursiveIteratorIterator::SELF_FIRST
28
        );
29
30
        foreach ($files as $file) {
31
            if (self::readablePhp($file)) {
32
                $class = self::getClassName($file);
33
34
                if (is_subclass_of($class, 'Illuminate\Database\Eloquent\Model')) {
35
                    self::$models[] = $class;
36
                }
37
            }
38
        }
39
40
        return self::$models;
41
    }
42
43
    /**
44
     * @param $file
45
     * @return bool
46
     */
47
    private static function readablePhp($file): bool
48
    {
49
        return
50
            $file->isReadable()
51
            && $file->isFile()
52
            && mb_strtolower($file->getExtension()) === 'php';
53
    }
54
55
    /**
56
     * @param string $path
57
     * @return string
58
     */
59
    protected static function getClassName(string $path): string
60
    {
61
        $traverser = new NodeTraverser();
62
        $traverser->addVisitor(new NameResolver());
63
64
        $root = collect(self::getStatements($path, $traverser))
65
            ->first(function ($statement) {
66
                return $statement instanceof Namespace_;
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Stmt\Namespace_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
            });
68
69
        if (!$root) {
70
            return '';
71
        }
72
73
        return self::findClass($root);
74
    }
75
76
    /**
77
     * @param string $path
78
     * @param NodeTraverser $traverser
79
     * @return array|\PhpParser\Node[]|\PhpParser\Node\Stmt[]|null
80
     */
81
    protected static function getStatements(string $path, NodeTraverser $traverser)
82
    {
83
        $statements = (new ParserFactory())
84
            ->create(ParserFactory::PREFER_PHP7)
85
            ->parse(file_get_contents($path));
86
87
        $statements = $traverser->traverse($statements);
88
89
        return $statements;
90
    }
91
92
    /**
93
     * @param $root
94
     * @return string
95
     */
96
    protected static function findClass($root): string
97
    {
98
        return collect($root->stmts)->filter(function ($statement) {
99
                return $statement instanceof Class_;
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Stmt\Class_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
100
            })->map(function (Class_ $statement) {
101
                return $statement->namespacedName->toString();
102
            })->first() ?? '';
103
    }
104
}
105