Skip to content

Commit 29f31e5

Browse files
Fix CS/WS issues
1 parent 699b15e commit 29f31e5

File tree

13 files changed

+96
-60
lines changed

13 files changed

+96
-60
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"phpunit/phpunit": "^7.0"
4242
},
4343
"suggest": {
44-
"ext-xdebug": "^2.5.5"
44+
"ext-xdebug": "^2.6.0"
4545
},
4646
"autoload": {
4747
"classmap": [

src/CodeCoverage.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use PHPUnit\Framework\TestCase;
1414
use PHPUnit\Runner\PhptTestCase;
15+
use PHPUnit\Util\Test;
1516
use SebastianBergmann\CodeCoverage\Driver\Driver;
1617
use SebastianBergmann\CodeCoverage\Driver\PHPDBG;
1718
use SebastianBergmann\CodeCoverage\Driver\Xdebug;
@@ -364,11 +365,11 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
364365
if ($id instanceof TestCase) {
365366
$_size = $id->getSize();
366367

367-
if ($_size === \PHPUnit\Util\Test::SMALL) {
368+
if ($_size === Test::SMALL) {
368369
$size = 'small';
369-
} elseif ($_size === \PHPUnit\Util\Test::MEDIUM) {
370+
} elseif ($_size === Test::MEDIUM) {
370371
$size = 'medium';
371-
} elseif ($_size === \PHPUnit\Util\Test::LARGE) {
372+
} elseif ($_size === Test::LARGE) {
372373
$size = 'large';
373374
}
374375

@@ -1113,7 +1114,7 @@ private function initializeData()
11131114

11141115
foreach ($this->filter->getWhitelist() as $file) {
11151116
if ($this->filter->isFile($file)) {
1116-
include_once($file);
1117+
include_once $file;
11171118
}
11181119
}
11191120

src/Driver/Driver.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ interface Driver
2020
*
2121
* @see http://xdebug.org/docs/code_coverage
2222
*/
23-
const LINE_EXECUTED = 1;
23+
public const LINE_EXECUTED = 1;
2424

2525
/**
2626
* @var int
2727
*
2828
* @see http://xdebug.org/docs/code_coverage
2929
*/
30-
const LINE_NOT_EXECUTED = -1;
30+
public const LINE_NOT_EXECUTED = -1;
3131

3232
/**
3333
* @var int
3434
*
3535
* @see http://xdebug.org/docs/code_coverage
3636
*/
37-
const LINE_NOT_EXECUTABLE = -2;
37+
public const LINE_NOT_EXECUTABLE = -2;
3838

3939
/**
4040
* Start collection of code coverage information.

src/Driver/PHPDBG.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ public function stop()
6666
\array_keys($fetchedLines)
6767
);
6868

69+
$sourceLines = [];
70+
6971
if ($newFiles) {
7072
$sourceLines = phpdbg_get_executable(
7173
['files' => $newFiles]
7274
);
73-
} else {
74-
$sourceLines = [];
7575
}
7676
}
7777

src/Driver/Xdebug.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,16 @@ final class Xdebug implements Driver
2727
private $cacheNumLines = [];
2828

2929
/**
30-
* Constructor.
30+
* @throws RuntimeException
3131
*/
3232
public function __construct()
3333
{
3434
if (!\extension_loaded('xdebug')) {
3535
throw new RuntimeException('This driver requires Xdebug');
3636
}
3737

38-
if (\version_compare(\phpversion('xdebug'), '2.2.1', '>=') &&
39-
!\ini_get('xdebug.coverage_enable')) {
40-
throw new RuntimeException(
41-
'xdebug.coverage_enable=On has to be set in php.ini'
42-
);
38+
if (!\ini_get('xdebug.coverage_enable')) {
39+
throw new RuntimeException('xdebug.coverage_enable=On has to be set in php.ini');
4340
}
4441
}
4542

@@ -65,6 +62,7 @@ public function start($determineUnusedAndDead = true)
6562
public function stop()
6663
{
6764
$data = \xdebug_get_code_coverage();
65+
6866
\xdebug_stop_code_coverage();
6967

7068
return $this->cleanup($data);

src/Node/Builder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ private function buildDirectoryStructure($files)
114114
$max = \count($path);
115115

116116
for ($i = 0; $i < $max; $i++) {
117+
$type = '';
118+
117119
if ($i == ($max - 1)) {
118120
$type = '/f';
119-
} else {
120-
$type = '';
121121
}
122122

123123
$pointer = &$pointer[$path[$i] . $type];
@@ -194,7 +194,7 @@ private function reducePaths(&$files)
194194
// strip phar:// prefixes
195195
if (\strpos($paths[$i], 'phar://') === 0) {
196196
$paths[$i] = \substr($paths[$i], 7);
197-
$paths[$i] = \strtr($paths[$i], '/', DIRECTORY_SEPARATOR);
197+
$paths[$i] = \str_replace('/', DIRECTORY_SEPARATOR, $paths[$i]);
198198
}
199199
$paths[$i] = \explode(DIRECTORY_SEPARATOR, $paths[$i]);
200200

src/Report/Clover.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function process(CodeCoverage $coverage, $target = null, $name = null)
4545

4646
$packages = [];
4747
$report = $coverage->getReport();
48-
unset($coverage);
4948

5049
foreach ($report as $item) {
5150
if (!$item instanceof File) {
@@ -57,10 +56,10 @@ public function process(CodeCoverage $coverage, $target = null, $name = null)
5756
$xmlFile = $xmlDocument->createElement('file');
5857
$xmlFile->setAttribute('name', $item->getPath());
5958

60-
$classes = $item->getClassesAndTraits();
61-
$coverage = $item->getCoverageData();
62-
$lines = [];
63-
$namespace = 'global';
59+
$classes = $item->getClassesAndTraits();
60+
$coverageData = $item->getCoverageData();
61+
$lines = [];
62+
$namespace = 'global';
6463

6564
foreach ($classes as $className => $class) {
6665
$classStatements = 0;
@@ -84,8 +83,8 @@ public function process(CodeCoverage $coverage, $target = null, $name = null)
8483
$methodCount = 0;
8584

8685
foreach (\range($method['startLine'], $method['endLine']) as $line) {
87-
if (isset($coverage[$line]) && ($coverage[$line] !== null)) {
88-
$methodCount = \max($methodCount, \count($coverage[$line]));
86+
if (isset($coverageData[$line]) && ($coverageData[$line] !== null)) {
87+
$methodCount = \max($methodCount, \count($coverageData[$line]));
8988
}
9089
}
9190

@@ -150,7 +149,7 @@ public function process(CodeCoverage $coverage, $target = null, $name = null)
150149
$xmlClass->appendChild($xmlMetrics);
151150
}
152151

153-
foreach ($coverage as $line => $data) {
152+
foreach ($coverageData as $line => $data) {
154153
if ($data === null || isset($lines[$line])) {
155154
continue;
156155
}

src/Report/Crap4j.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ public function process(CodeCoverage $coverage, $target = null, $name = null)
117117
$stats->appendChild($document->createElement('crapLoad', \round($fullCrapLoad)));
118118
$stats->appendChild($document->createElement('totalCrap', $fullCrap));
119119

120+
$crapMethodPercent = 0;
121+
120122
if ($fullMethodCount > 0) {
121123
$crapMethodPercent = $this->roundValue((100 * $fullCrapMethodCount) / $fullMethodCount);
122-
} else {
123-
$crapMethodPercent = 0;
124124
}
125125

126126
$stats->appendChild($document->createElement('crapMethodPercent', $crapMethodPercent));
@@ -131,8 +131,8 @@ public function process(CodeCoverage $coverage, $target = null, $name = null)
131131
$buffer = $document->saveXML();
132132

133133
if ($target !== null) {
134-
if (!\is_dir(\dirname($target))) {
135-
\mkdir(\dirname($target), 0777, true);
134+
if (!\mkdir(\dirname($target)) && !\is_dir(\dirname($target))) {
135+
throw new \RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
136136
}
137137

138138
\file_put_contents($target, $buffer);

src/Report/Html/Renderer/Dashboard.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,11 @@ protected function projectRisks(array $classes, $baseLink)
248248

249249
foreach ($classes as $className => $class) {
250250
foreach ($class['methods'] as $methodName => $method) {
251-
if ($method['coverage'] < $this->highLowerBound &&
252-
$method['ccn'] > 1) {
251+
if ($method['coverage'] < $this->highLowerBound && $method['ccn'] > 1) {
252+
$key = $methodName;
253+
253254
if ($className !== '*') {
254255
$key = $className . '::' . $methodName;
255-
} else {
256-
$key = $methodName;
257256
}
258257

259258
$methodRisks[$key] = $method['crap'];

src/Report/Html/Renderer/File.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,14 @@ protected function renderSource(FileNode $node)
394394
}
395395
}
396396

397+
$popover = '';
398+
397399
if (!empty($popoverTitle)) {
398400
$popover = \sprintf(
399401
' data-title="%s" data-content="%s" data-placement="bottom" data-html="true"',
400402
$popoverTitle,
401403
\htmlspecialchars($popoverContent)
402404
);
403-
} else {
404-
$popover = '';
405405
}
406406

407407
$lines .= \sprintf(

src/Report/Text.php

+60-21
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,55 @@
2121
*/
2222
final class Text
2323
{
24+
/**
25+
* @var string
26+
*/
27+
private const COLOR_GREEN = "\x1b[30;42m";
28+
29+
/**
30+
* @var string
31+
*/
32+
private const COLOR_YELLOW = "\x1b[30;43m";
33+
34+
/**
35+
* @var string
36+
*/
37+
private const COLOR_RED = "\x1b[37;41m";
38+
39+
/**
40+
* @var string
41+
*/
42+
private const COLOR_HEADER = "\x1b[1;37;40m";
43+
44+
/**
45+
* @var string
46+
*/
47+
private const COLOR_RESET = "\x1b[0m";
48+
49+
/**
50+
* @var string
51+
*/
52+
private const COLOR_EOL = "\x1b[2K";
53+
54+
/**
55+
* @var int
56+
*/
2457
private $lowUpperBound;
58+
59+
/**
60+
* @var int
61+
*/
2562
private $highLowerBound;
63+
64+
/**
65+
* @var bool
66+
*/
2667
private $showUncoveredFiles;
27-
private $showOnlySummary;
2868

29-
private $colors = [
30-
'green' => "\x1b[30;42m",
31-
'yellow' => "\x1b[30;43m",
32-
'red' => "\x1b[37;41m",
33-
'header' => "\x1b[1;37;40m",
34-
'reset' => "\x1b[0m",
35-
'eol' => "\x1b[2K",
36-
];
69+
/**
70+
* @var bool
71+
*/
72+
private $showOnlySummary;
3773

3874
/**
3975
* @param int $lowUpperBound
@@ -59,7 +95,6 @@ public function process(CodeCoverage $coverage, $showColors = false)
5995
{
6096
$output = PHP_EOL . PHP_EOL;
6197
$report = $coverage->getReport();
62-
unset($coverage);
6398

6499
$colors = [
65100
'header' => '',
@@ -75,17 +110,20 @@ public function process(CodeCoverage $coverage, $showColors = false)
75110
$report->getNumTestedClassesAndTraits(),
76111
$report->getNumClassesAndTraits()
77112
);
113+
78114
$colors['methods'] = $this->getCoverageColor(
79115
$report->getNumTestedMethods(),
80116
$report->getNumMethods()
81117
);
118+
82119
$colors['lines'] = $this->getCoverageColor(
83120
$report->getNumExecutedLines(),
84121
$report->getNumExecutableLines()
85122
);
86-
$colors['reset'] = $this->colors['reset'];
87-
$colors['header'] = $this->colors['header'];
88-
$colors['eol'] = $this->colors['eol'];
123+
124+
$colors['reset'] = self::COLOR_RESET;
125+
$colors['header'] = self::COLOR_HEADER;
126+
$colors['eol'] = self::COLOR_EOL;
89127
}
90128

91129
$classes = \sprintf(
@@ -169,17 +207,18 @@ public function process(CodeCoverage $coverage, $showColors = false)
169207
$classMethods++;
170208
$classStatements += $method['executableLines'];
171209
$coveredClassStatements += $method['executedLines'];
210+
172211
if ($method['coverage'] == 100) {
173212
$coveredMethods++;
174213
}
175214
}
176215

216+
$namespace = '';
217+
177218
if (!empty($class['package']['namespace'])) {
178219
$namespace = '\\' . $class['package']['namespace'] . '::';
179220
} elseif (!empty($class['package']['fullPackage'])) {
180221
$namespace = '@' . $class['package']['fullPackage'] . '::';
181-
} else {
182-
$namespace = '';
183222
}
184223

185224
$classCoverage[$namespace . $className] = [
@@ -200,8 +239,7 @@ public function process(CodeCoverage $coverage, $showColors = false)
200239
$resetColor = '';
201240

202241
foreach ($classCoverage as $fullQualifiedPath => $classInfo) {
203-
if ($classInfo['statementsCovered'] != 0 ||
204-
$this->showUncoveredFiles) {
242+
if ($this->showUncoveredFiles || $classInfo['statementsCovered'] != 0) {
205243
if ($showColors) {
206244
$methodColor = $this->getCoverageColor($classInfo['methodsCovered'], $classInfo['methodCount']);
207245
$linesColor = $this->getCoverageColor($classInfo['statementsCovered'], $classInfo['statementCount']);
@@ -226,13 +264,14 @@ private function getCoverageColor($numberOfCoveredElements, $totalNumberOfElemen
226264
);
227265

228266
if ($coverage >= $this->highLowerBound) {
229-
return $this->colors['green'];
267+
return self::COLOR_GREEN;
230268
}
269+
231270
if ($coverage > $this->lowUpperBound) {
232-
return $this->colors['yellow'];
271+
return self::COLOR_YELLOW;
233272
}
234273

235-
return $this->colors['red'];
274+
return self::COLOR_RED;
236275
}
237276

238277
private function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $precision)
@@ -251,7 +290,7 @@ private function printCoverageCounts($numberOfCoveredElements, $totalNumberOfEle
251290

252291
private function format($color, $padding, $string)
253292
{
254-
$reset = $color ? $this->colors['reset'] : '';
293+
$reset = $color ? self::COLOR_RESET : '';
255294

256295
return $color . \str_pad($string, $padding) . $reset . PHP_EOL;
257296
}

src/Report/Xml/Coverage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(\DOMElement $context, $line)
3535

3636
$this->writer = new \XMLWriter();
3737
$this->writer->openMemory();
38-
$this->writer->startElementNs(null, $context->nodeName, 'http://schema.phpunit.de/coverage/1.0');
38+
$this->writer->startElementNS(null, $context->nodeName, 'http://schema.phpunit.de/coverage/1.0');
3939
$this->writer->writeAttribute('nr', $line);
4040
}
4141

0 commit comments

Comments
 (0)