Completed
Pull Request — master (#41)
by Innocent
01:33
created

ErrorPrinter::printHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope;
4
5
use Imanghafoori\LaravelMicroscope\PendingObjects\PendingError;
6
7
class ErrorPrinter
8
{
9
    public $counts
10
        = [
11
            'view'                => [],
12
            'route'               => [],
13
            'total'               => 0,
14
            'bladeImport'         => [],
15
            'badRelation'         => [],
16
            'wrongImport'         => [],
17
            'wrongUsedClassError' => [],
18
            'badNamespace'        => [],
19
            'others'              => [],
20
        ];
21
22
    public $printer;
23
24
    public $logErrors = true;
25
26 View Code Duplication
    public function view($path, $lineContent, $lineNumber, $fileName)
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...
27
    {
28
        array_push($this->counts['view'], (new PendingError('view'))
29
            ->header($this->yellow($fileName.'.blade.php').' does not exist')
30
            ->errorData(trim($lineContent))
31
            ->link($path, $lineNumber));
32
    }
33
34 View Code Duplication
    public function others($error, $header = null, $linkPath = null, $linkLineNum = 0)
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...
35
    {
36
        array_push($this->counts['others'], (new PendingError('others'))
37
            ->header($header)
38
            ->errorData($error)
39
            ->link($linkPath, $linkLineNum));
40
    }
41
42 View Code Duplication
    public function route($path, $errorIt, $errorTxt, $linkPath = null, $linkLineNum = 0)
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...
43
    {
44
        array_push($this->counts['route'], (new PendingError('route'))
45
            ->header($errorIt)
46
            ->errorData($errorTxt.$this->yellow($path))
47
            ->link($linkPath, $linkLineNum));
48
    }
49
50
    public function bladeImport($class, $blade)
51
    {
52
        array_push($this->counts['bladeImport'], (new PendingError('bladeImport'))
53
            ->header('Class does not exist in blade file:')
54
            ->errorData($this->yellow($class['class']).' <==== does not exist')
55
            ->link($blade->getPathname(), $class['line']));
56
    }
57
58
    public function authConf()
59
    {
60
        $this->print('The model in the "config/auth.php" is not a valid class');
61
    }
62
63
    public function badRelation(\ReflectionClass $ref, \ReflectionMethod $method, $relatedModel)
64
    {
65
        array_push($this->counts['badRelation'], (new PendingError('badRelation'))
66
            ->header('Wrong model is passed in relation:')
67
            ->errorData($this->yellow($relatedModel).'   <==== does not exist')
68
            ->link($ref->getFileName(), $method->getStartLine() + 1));
69
    }
70
71 View Code Duplication
    public function wrongImport($absPath, $class, $line)
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...
72
    {
73
        array_push($this->counts['wrongImport'], (new PendingError('wrongImport'))
74
            ->header('Wrong import:')
75
            ->errorData($this->yellow("use $class;").'   <==== does not exist. ')
76
            ->link($absPath, $line));
77
    }
78
79
    public function wrongUsedClassError($absFilePath, $nonImportedClass)
80
    {
81
        array_push($this->counts['wrongUsedClassError'], (new PendingError('wrongUsedClassError'))
82
            ->header('Class does not exist:')
83
            ->errorData($this->yellow($nonImportedClass['class']).'  <==== does not exist.')
84
            ->link($absFilePath, $nonImportedClass['line']));
85
    }
86
87
    public function yellow($msg)
88
    {
89
        return "<fg=yellow>$msg</>";
90
    }
91
92
    /**
93
     * @param  string  $classPath
94
     * @param  string  $correctNamespace
95
     * @param  string  $incorrectNamespace
96
     *
97
     * @return void
98
     */
99
    public function badNamespace($classPath, $correctNamespace, $incorrectNamespace)
100
    {
101
        $this->printHeader('Incorrect namespace: '.$this->yellow("namespace $incorrectNamespace;"));
102
        $this->print('Correct namespace:   '.$this->yellow("namespace $correctNamespace;"));
103
        $this->printLink($classPath);
104
        $this->end();
105
        $this->counts['badNamespace']++;
106
    }
107
108
    public function print($msg, $path = '  |    ', $len = 81)
109
    {
110
        $msgLen = strlen($msg);
111
        if (strpos($msg, 'yellow')) {
112
            $msgLen = $msgLen - 14;
113
        }
114
        $len = $len - $msgLen;
115
        if ($len < 0) {
116
            $len = 0;
117
        }
118
119
        $this->printer->writeln($path.$msg.str_repeat(' ', $len).'|  ');
120
    }
121
122
    public function printHeader($msg)
123
    {
124
        $this->print('');
125
        $number = ++$this->counts['total'];
126
        $number = '<fg=yellow>'.$number.' </>';
127
        $path = "  | $number";
128
129
        $this->print($msg, $path);
130
    }
131
132
    public function end()
133
    {
134
        $this->print('');
135
        $this->printer->writeln('  |'.str_repeat('*', 85).'|  ');
136
    }
137
138
    public function printLink($path, $lineNumber = 4)
139
    {
140
        if ($path) {
141
            $filePath = trim(str_replace(base_path(), '', $path), '\\/');
142
            $this->print('at <fg=green>'.$filePath.'</>'.':<fg=green>'.$lineNumber.'</>', '', 114);
143
        }
144
    }
145
146
    public function fixedNamespace($correctNamespace)
147
    {
148
        $msg = $this->yellow("namespace $correctNamespace;");
149
        $this->print('namespace fixed to: '.$msg);
150
        $this->end();
151
    }
152
153
    /**
154
     * Checks for errors for the run command.
155
     *
156
     * @return int
157
     */
158
    public function hasErrors()
159
    {
160
        $errorsCollection = collect($this->counts);
161
162
        return $errorsCollection
163
            ->flatten()
164
            ->filter(function ($action) {
165
                return $action instanceof PendingError;
166
            })->count();
167
    }
168
169
    /**
170
     * Logs the errors to the console.
171
     */
172
    public function logErrors()
173
    {
174
        $errorsCollection = collect($this->counts)
0 ignored issues
show
Unused Code introduced by
$errorsCollection is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175
            ->except('total')
176
            ->flatten()
177
            ->each(function ($error) {
178
                if ($error instanceof PendingError) {
179
                    $this->printHeader($error->getHeader());
180
                    $this->print($error->getErrorData());
181
                    $this->printLink($error->getLinkPath(), $error->getLinkLineNumber());
182
                    $this->end();
183
                }
184
            });
185
    }
186
}
187