CheckViewFilesExistence   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 16 4
A isEnvMake() 0 14 3
A error() 0 4 1
A isVariable() 0 4 2
A isMethodCall() 0 4 2
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Checks;
4
5
use Illuminate\Support\Facades\View;
6
use Imanghafoori\LaravelMicroscope\Commands\CheckViews;
7
use Imanghafoori\LaravelMicroscope\ErrorTypes\BladeFile;
8
9
class CheckViewFilesExistence
10
{
11
    public static function check($tokens, $absPath)
12
    {
13
        $tCount = \count($tokens);
14
        for ($i = 0; $i < $tCount; $i++) {
15
            if (! self::isEnvMake($tokens, $i)) {
16
                continue;
17
            }
18
19
            $viewName = \trim($tokens[$i + 4][1], '\'\"');
20
            if (! View::exists($viewName)) {
21
                CheckViews::$checkedCallsNum++;
22
                self::error($tokens, $absPath, $i);
23
            }
24
            $i = $i + 5;
25
        }
26
    }
27
28
    private static function isEnvMake($tokens, $i)
29
    {
30
        $varName = '$__env';
31
        $methods = [
32
            'make',
33
            'first',
34
            'renderWhen',
35
        ];
36
37
        // checks for this syntax: $__env->make('myViewFile', ...
38
        return self::isMethodCall($tokens, $i, $varName, $methods)
39
            && ($tokens[$i + 4][0] ?? '') == T_CONSTANT_ENCAPSED_STRING
40
            && ($tokens[$i + 5] ?? null) == ',';
41
    }
42
43
    private static function error($tokens, $absPath, $i)
44
    {
45
        BladeFile::isMissing($absPath, $tokens[$i + 4][2], $tokens[$i + 4][1]);
46
    }
47
48
    private static function isVariable($token, string $varName)
49
    {
50
        return ($token[0] == T_VARIABLE) && ($token[1] == $varName);
51
    }
52
53
    private static function isMethodCall($tokens, $i, $varName, $methods)
54
    {
55
        return self::isVariable($tokens[$i], $varName) && \in_array($tokens[$i + 2][1] ?? null, $methods);
56
    }
57
}
58