ConsolePrinterInstaller::compactCall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 10
Ratio 76.92 %

Importance

Changes 0
Metric Value
dl 10
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\ErrorReporters;
4
5
use Illuminate\Support\Facades\Event;
6
use Illuminate\Support\Str;
7
use Imanghafoori\LaravelMicroscope\ErrorTypes\BladeFile;
8
use Imanghafoori\LaravelMicroscope\ErrorTypes\CompactCall;
9
use Imanghafoori\LaravelMicroscope\ErrorTypes\ddFound;
10
use Imanghafoori\LaravelMicroscope\ErrorTypes\EnvFound;
11
use Imanghafoori\LaravelMicroscope\ErrorTypes\RouteDefinitionConflict;
12
13
class ConsolePrinterInstaller
14
{
15
    protected static function finishCommand($command)
16
    {
17
        /**
18
         * @var $errorPrinter ErrorPrinter
19
         */
20
        $errorPrinter = app(ErrorPrinter::class);
21
        $errorPrinter->printer = $command->getOutput();
22
23
        $commandName = class_basename($command);
24
        $commandType = Str::after($commandName, 'Check');
25
        $commandType = strtolower($commandType);
26
27
        if (! $errorPrinter->logErrors) {
28
            return;
29
        }
30
31
        if (($errorCount = $errorPrinter->hasErrors()) || $errorPrinter->pended) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errorPrinter->pended 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...
32
            $lastTimeCount = cache()->get(self::getKey($commandType), null);
33
34
            $errorCount && $command->getOutput()->writeln(PHP_EOL.$errorCount.' errors found for '.$commandType);
35
            $errorPrinter->logErrors();
36
            if (! is_null($lastTimeCount)) {
37
                $_msg2 = PHP_EOL.self::printErrorCount($lastTimeCount, $commandType, $errorCount);
38
                $_msg2 && $command->info($_msg2);
39
            }
40
        } else {
41
            $command->info(PHP_EOL.'All '.$commandType.' are correct!');
42
        }
43
        cache()->set(self::getKey($commandType), $errorCount);
44
45
        $errorPrinter->printTime();
46
    }
47
48
    protected static function getKey($commandType)
49
    {
50
        return "__microscope__$commandType-count";
51
    }
52
53
    public static function boot()
54
    {
55 View Code Duplication
        Event::listen(BladeFile::class, function (BladeFile $event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
            $data = $event->data;
57
            $msg = 'The blade file is missing:';
58
59
            app(ErrorPrinter::class)->view(
60
                $data['absPath'],
61
                $msg,
62
                $data['lineNumber'],
63
                $data['name']
64
            );
65
        });
66
67 View Code Duplication
        Event::listen(ddFound::class, function (ddFound $event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
            $data = $event->data;
69
            app(ErrorPrinter::class)->simplePendError(
70
                $data['name'],
71
                $data['absPath'],
72
                $data['lineNumber'],
73
                'ddFound',
74
                'Debug function found: '
75
            );
76
        });
77
78
        self::compactCall();
79
80
        Event::listen(RouteDefinitionConflict::class, function ($e) {
81
            app(ErrorPrinter::class)->routeDefinitionConflict(
82
                $e->data['poorRoute'],
83
                $e->data['bullyRoute'],
84
                $e->data['info']
85
            );
86
        });
87
88 View Code Duplication
        Event::listen(EnvFound::class, function (EnvFound $event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            $data = $event->data;
90
            app(ErrorPrinter::class)->simplePendError(
91
                $data['name'],
92
                $data['absPath'],
93
                $data['lineNumber'],
94
                'envFound',
95
                'env() function found: '
96
            );
97
        });
98
99
        Event::listen('microscope.finished.checks', function ($command) {
100
            self::finishCommand($command);
101
        });
102
    }
103
104
    private static function compactCall()
105
    {
106 View Code Duplication
        Event::listen(CompactCall::class, function ($event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107
            $data = $event->data;
108
109
            app(ErrorPrinter::class)->compactError(
110
                $data['absPath'],
111
                $data['lineNumber'],
112
                $data['name'],
113
                'CompactCall',
114
                'compact() function call has problems man ! ');
115
        });
116
    }
117
118
    protected static function printErrorCount($lastTimeCount, $commandType, $errorCount)
119
    {
120
        $lastTimeError = $commandType.' errors, compared to the last run.';
121
        if (($errorCount > $lastTimeCount)) {
122
            return ' +'.($errorCount - $lastTimeCount).' new '.$lastTimeError;
123
        } elseif ($errorCount < $lastTimeCount) {
124
            return ' -'.($lastTimeCount - $errorCount).' less '.$lastTimeError;
125
        }
126
    }
127
}
128