Skip to content

Commit 1c860dc

Browse files
committed
Refactored the bin files
Added new file mapper
1 parent 623c8a1 commit 1c860dc

File tree

7 files changed

+211
-158
lines changed

7 files changed

+211
-158
lines changed

bin/phpcsDiffFilter

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,17 @@
11
#!/usr/bin/env php
22
<?php
3-
$locations = [
4-
__DIR__ . '/../vendor/autoload.php',
5-
__DIR__ . '/vendor/autoload.php',
6-
__DIR__ . '/../autoload.php'
7-
];
3+
namespace exussum12\CoverageChecker;
84

9-
$found = false;
5+
include (__DIR__ . "/../functions.php");
106

11-
foreach ($locations as $file) {
12-
if (file_exists($file)) {
13-
require_once($file);
14-
$found = true;
15-
break;
16-
}
17-
}
7+
setUp();
8+
$minimumPercentCovered = getMinPercent();
189

19-
if (!$found) {
20-
error_log(
21-
"Can't find the autoload file," .
22-
"please make sure 'composer install' has been run"
23-
);
24-
25-
exit(1);
26-
}
27-
28-
if (!isset($argv[1], $argv[2])) {
29-
error_log(
30-
"Missing arguments, please call with diff and check file"
31-
);
32-
exit(1);
33-
}
34-
35-
if ($argv[1] == "-") {
36-
$argv[1] = "php://stdin";
37-
}
38-
if ($argv[2] == "-") {
39-
$argv[2] = "php://stdin";
40-
}
41-
42-
$matcher = new exussum12\CoverageChecker\FileMatchers\EndsWith();
43-
$diff = new exussum12\CoverageChecker\DiffFileLoader($argv[1]);
44-
$phpunit = new exussum12\CoverageChecker\PhpCsLoader($argv[2]);
45-
$coverageCheck = new exussum12\CoverageChecker\CoverageCheck($diff, $phpunit, $matcher);
10+
$matcher = new FileMatchers\EndsWith();
11+
$diff = new DiffFileLoader($argv[1]);
12+
$phpunit = new PhpCsLoader($argv[2]);
13+
$coverageCheck = new CoverageCheck($diff, $phpunit, $matcher);
4614

4715
$lines = $coverageCheck->getCoveredLines();
4816

49-
$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);
50-
51-
if (empty($uncoveredLines)) {
52-
exit(0);
53-
}
54-
55-
print_r($lines['uncoveredLines']);
56-
exit(2);
17+
handleOutput($lines, $minimumPercentCovered);

bin/phpmdDiffFilter

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,17 @@
11
#!/usr/bin/env php
22
<?php
3-
$locations = [
4-
__DIR__ . '/../vendor/autoload.php',
5-
__DIR__ . '/vendor/autoload.php',
6-
__DIR__ . '/../autoload.php'
7-
];
3+
namespace exussum12\CoverageChecker;
84

9-
$found = false;
5+
include (__DIR__ . "/../functions.php");
106

11-
foreach ($locations as $file) {
12-
if (file_exists($file)) {
13-
require_once($file);
14-
$found = true;
15-
break;
16-
}
17-
}
7+
setUp();
8+
$minimumPercentCovered = getMinPercent();
189

19-
if (!$found) {
20-
error_log(
21-
"Can't find the autoload file," .
22-
"please make sure 'composer install' has been run"
23-
);
24-
25-
exit(1);
26-
}
27-
28-
if (!isset($argv[1], $argv[2])) {
29-
error_log(
30-
"Missing arguments, please call with diff and check file"
31-
);
32-
exit(1);
33-
}
34-
35-
if ($argv[1] == "-") {
36-
$argv[1] = "php://stdin";
37-
}
38-
if ($argv[2] == "-") {
39-
$argv[2] = "php://stdin";
40-
}
41-
42-
$matcher = new exussum12\CoverageChecker\FileMatchers\EndsWith();
43-
$diff = new exussum12\CoverageChecker\DiffFileLoader($argv[1]);
44-
$phpunit = new exussum12\CoverageChecker\PhpMdLoader($argv[2]);
45-
$coverageCheck = new exussum12\CoverageChecker\CoverageCheck($diff, $phpunit, $matcher);
10+
$matcher = new FileMatchers\EndsWith();
11+
$diff = new DiffFileLoader($argv[1]);
12+
$phpunit = new PhpMdLoader($argv[2]);
13+
$coverageCheck = new CoverageCheck($diff, $phpunit, $matcher);
4614

4715
$lines = $coverageCheck->getCoveredLines();
4816

49-
$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);
50-
51-
if (empty($uncoveredLines)) {
52-
exit(0);
53-
}
54-
55-
print_r($lines['uncoveredLines']);
56-
exit(2);
17+
handleOutput($lines, $minimumPercentCovered);

bin/phpunitDiffFilter

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,17 @@
11
#!/usr/bin/env php
22
<?php
3-
$locations = [
4-
__DIR__ . '/../vendor/autoload.php',
5-
__DIR__ . '/vendor/autoload.php',
6-
__DIR__ . '/../autoload.php'
7-
];
3+
namespace exussum12\CoverageChecker;
84

9-
$found = false;
5+
require_once (__DIR__ . "/../functions.php");
106

11-
foreach ($locations as $file) {
12-
if (file_exists($file)) {
13-
require_once($file);
14-
$found = true;
15-
break;
16-
}
17-
}
7+
setUp();
8+
$minimumPercentCovered = getMinPercent();
189

19-
if (!$found) {
20-
error_log(
21-
"Can't find the autoload file," .
22-
"please make sure 'composer install' has been run"
23-
);
24-
25-
exit(1);
26-
}
27-
28-
if (!isset($argv[1], $argv[2])) {
29-
error_log(
30-
"Missing arguments, please call with diff and check file"
31-
);
32-
exit(1);
33-
}
34-
35-
if ($argv[1] == "-") {
36-
$argv[1] = "php://stdin";
37-
}
38-
if ($argv[2] == "-") {
39-
$argv[2] = "php://stdin";
40-
}
41-
42-
$minimumPercentCovered = 100;
43-
if (isset($argv[3])) {
44-
$minimumPercentCovered = min($minimumPercentCovered, max(0, $argv[3]));
45-
}
46-
47-
48-
$matcher = new exussum12\CoverageChecker\FileMatchers\EndsWith();
49-
$diff = new exussum12\CoverageChecker\DiffFileLoader($argv[1]);
50-
$phpunit = new exussum12\CoverageChecker\XMLReport($argv[2]);
51-
$coverageCheck = new exussum12\CoverageChecker\CoverageCheck($diff, $phpunit, $matcher);
10+
$matcher = new FileMatchers\EndsWith();
11+
$diff = new DiffFileLoader($argv[1]);
12+
$phpunit = new XMLReport($argv[2]);
13+
$coverageCheck = new CoverageCheck($diff, $phpunit, $matcher);
5214

5315
$lines = $coverageCheck->getCoveredLines();
5416

55-
$coveredLines = count($lines['coveredLines'], COUNT_RECURSIVE);
56-
$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);
57-
58-
if($coveredLines + $uncoveredLines == 0) {
59-
exit(0);
60-
}
61-
62-
$percentCovered = 100 * ($coveredLines / ($coveredLines + $uncoveredLines));
63-
if ($percentCovered >= $minimumPercentCovered) {
64-
exit(0);
65-
}
66-
67-
echo "$percentCovered% Covered, Missed lines " . PHP_EOL;
68-
print_r($lines['uncoveredLines']);
69-
exit(2);
17+
handleOutput($lines, $minimumPercentCovered);

functions.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
namespace exussum12\CoverageChecker;
3+
4+
function setUp()
5+
{
6+
findAutoLoader();
7+
checkCallIsCorrect();
8+
adjustForStdIn();
9+
}
10+
11+
function findAutoLoader()
12+
{
13+
$locations = [
14+
__DIR__ . '/vendor/autoload.php',
15+
__DIR__ . '/autoload.php'
16+
];
17+
18+
$found = false;
19+
20+
foreach ($locations as $file) {
21+
if (file_exists($file)) {
22+
require_once($file);
23+
$found = true;
24+
break;
25+
}
26+
}
27+
28+
if (!$found) {
29+
error_log(
30+
"Can't find the autoload file," .
31+
"please make sure 'composer install' has been run"
32+
);
33+
34+
exit(1);
35+
}
36+
}
37+
38+
function checkCallIsCorrect()
39+
{
40+
global $argv;
41+
if (!isset($argv[1], $argv[2])) {
42+
error_log(
43+
"Missing arguments, please call with diff and check file"
44+
);
45+
exit(1);
46+
}
47+
}
48+
49+
function adjustForStdIn()
50+
{
51+
global $argv;
52+
foreach ([1, 2] as $arg) {
53+
if ($argv[$arg] == "-") {
54+
$argv[$arg] = "php://stdin";
55+
}
56+
}
57+
}
58+
59+
function getMinPercent()
60+
{
61+
global $argv;
62+
$minimumPercentCovered = 100;
63+
if (isset($argv[3])) {
64+
$minimumPercentCovered = min($minimumPercentCovered, max(0, $argv[3]));
65+
return $minimumPercentCovered;
66+
}
67+
return $minimumPercentCovered;
68+
}
69+
70+
function handleOutput($lines, $minimumPercentCovered)
71+
{
72+
$coveredLines = count($lines['coveredLines'], COUNT_RECURSIVE);
73+
$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);
74+
75+
if ($coveredLines + $uncoveredLines == 0) {
76+
exit(0);
77+
}
78+
79+
$percentCovered = 100 * ($coveredLines / ($coveredLines + $uncoveredLines));
80+
if ($percentCovered >= $minimumPercentCovered) {
81+
exit(0);
82+
}
83+
84+
echo "$percentCovered% Covered, Missed lines " . PHP_EOL;
85+
print_r($lines['uncoveredLines']);
86+
exit(2);
87+
}

src/FileMatchers/EndsWith.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function match($needle, array $haystack)
2626
}
2727

2828
/**
29-
* Month for finding if two strings end in the same way
29+
* Find if two strings end in the same way
3030
* @param $haystack
3131
* @param $needle
3232
* @return bool

src/FileMatchers/FileMapper.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
namespace exussum12\CoverageChecker\FileMatchers;
3+
4+
use exussum12\CoverageChecker\FileMatcher;
5+
use exussum12\CoverageChecker\Exceptions\FileNotFound;
6+
7+
/**
8+
* Class FileMapper
9+
* @package exussum12\CoverageChecker\FileMatchers
10+
*/
11+
class FileMapper implements FileMatcher
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $originalPath;
17+
/**
18+
* @var string
19+
*/
20+
protected $newPath;
21+
22+
/**
23+
* FileMapper constructor.
24+
* @param string $originalPath
25+
* @param string $newPath
26+
*/
27+
public function __construct($originalPath, $newPath)
28+
{
29+
$this->originalPath = $originalPath;
30+
$this->newPath = $newPath;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function match($needle, array $haystack)
37+
{
38+
foreach ($haystack as $file) {
39+
if ($this->checkMapping($file, $needle)) {
40+
return $file;
41+
}
42+
}
43+
44+
throw new FileNotFound();
45+
}
46+
47+
/**
48+
* @param string $file
49+
* @param string $needle
50+
* @return bool
51+
*/
52+
private function checkMapping($file, $needle)
53+
{
54+
return $file == str_replace(
55+
$this->originalPath,
56+
$this->newPath,
57+
$needle
58+
);
59+
}
60+
}

0 commit comments

Comments
 (0)