Passed
Push — master ( a2c862...16044f )
by Robert
05:35
created

LiveEngageTestCoverage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LivePersonInc\LiveEngageLaravel\Commands;
4
5
use Illuminate\Console\Command;
6
use Orchestra\Parser\Xml\Facade as XmlParser;
0 ignored issues
show
Bug introduced by
The type Orchestra\Parser\Xml\Facade was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Nathanmac\Utilities\Parser\Facades\Parser;
8
9
/**
10
 * @codeCoverageIgnore
11
 */
12
class LiveEngageTestCoverage extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'liveperson:le:coverage {--lines}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Reports the test coverage of this package';
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
	    $path = __DIR__ . "/../../tests/coverage.xml";
46
	    if (!file_exists($path)) {
47
		    $this->error("Coverage file does not exist.");
48
	    } else {
49
		    
50
		    $xml = Parser::xml(file_get_contents($path));
51
		    
52
		    
53
		    $elements = [
54
			    'covered' => $xml['project']['metrics']['@coveredelements'],
55
				'total' => $xml['project']['metrics']['@elements']
56
		    ];
57
		    
58
		    $statements = [
59
			    'covered' => $xml['project']['metrics']['@coveredstatements'],
60
				'total' => $xml['project']['metrics']['@statements']
61
		    ];
62
		    
63
		    $methods = [
64
			    'covered' => $xml['project']['metrics']['@coveredmethods'],
65
				'total' => $xml['project']['metrics']['@methods']
66
		    ];
67
		    
68
		    $percentages = [
69
			    'elements' => round(($elements['covered'] / $elements['total']) * 100),
70
			    'statements' => round(($statements['covered'] / $statements['total']) * 100),
71
			    'methods' => round(($methods['covered'] / $methods['total']) * 100),
72
		    ];
73
		    
74
		    $average = round(($percentages['elements'] + $percentages['statements'] + $percentages['methods']) / 3);
75
		    $percentages['average'] = $average;
76
		    
77
		    $percentages = array_map(function($item) {
78
			    return number_format($item) . '%';
79
			}, $percentages);
80
			
81
			$headers = ['Elements', 'Statements', 'Methods', 'Average'];
82
		    $this->table($headers, [$percentages]);
83
		    
84
		    if ($this->option('lines')) {
85
		    
86
			    $packages = $xml['project']['package'];
87
			    
88
			    foreach ($packages as $namespace) {
89
				    $name = $namespace['@name'];
90
				    if (!$this->isAssoc($namespace['file'])) {
91
					    foreach ($namespace['file'] as $file) {
92
						    $this->processFile($file, $name);
93
					    }
94
					} else {
95
						$this->processFile($namespace['file'], $name);
96
					}
97
			    }
98
		    
99
		    }
100
		    		    
101
	    }
102
        
103
    }
104
    
105
    private function processFile($file, $parent)
0 ignored issues
show
Unused Code introduced by
The parameter $parent is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
    private function processFile($file, /** @scrutinizer ignore-unused */ $parent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
	    $filename = $file['@name'];
108
	    $name = basename($file['@name']);
109
	    if (!isset($file['line'])) return;
110
	    foreach ($file['line'] as $line) {
111
		    if ($line['@count'] == 0 && $line['@type'] == 'stmt') {
112
			    $text = file_get_contents($filename);
113
			    $text = str_replace("\t", "  ", $text);
114
			    $filecontents = explode("\n", $text);
115
			    $number = $line['@num'];
116
			    $this->warn($name);
117
	    
118
			    $this->line("+---------------------------------------------------------");
119
			    $this->line("|");
120
			    $this->line("|" . ($number - 1) . $filecontents[$number - 2]);
121
			    $this->info("|" . $number . $filecontents[$number - 1]);
122
			    $this->line("|" . ($number + 1) . $filecontents[$number]);
123
			    
124
			    $this->line("+---------------------------------------------------------");
125
		    }
126
	    }
127
    }
128
    
129
    private function isAssoc(array $arr)
130
	{
131
	    if (array() === $arr) return false;
132
	    return array_keys($arr) !== range(0, count($arr) - 1);
133
	}
134
}
135