Skip to content

Commit

Permalink
Refactored the bin files
Browse files Browse the repository at this point in the history
Added new file mapper
  • Loading branch information
exussum12 committed Jan 12, 2017
1 parent 623c8a1 commit 1c860dc
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 158 deletions.
57 changes: 9 additions & 48 deletions bin/phpcsDiffFilter
Original file line number Diff line number Diff line change
@@ -1,56 +1,17 @@
#!/usr/bin/env php
<?php
$locations = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/../autoload.php'
];
namespace exussum12\CoverageChecker;

$found = false;
include (__DIR__ . "/../functions.php");

foreach ($locations as $file) {
if (file_exists($file)) {
require_once($file);
$found = true;
break;
}
}
setUp();
$minimumPercentCovered = getMinPercent();

if (!$found) {
error_log(
"Can't find the autoload file," .
"please make sure 'composer install' has been run"
);

exit(1);
}

if (!isset($argv[1], $argv[2])) {
error_log(
"Missing arguments, please call with diff and check file"
);
exit(1);
}

if ($argv[1] == "-") {
$argv[1] = "php://stdin";
}
if ($argv[2] == "-") {
$argv[2] = "php://stdin";
}

$matcher = new exussum12\CoverageChecker\FileMatchers\EndsWith();
$diff = new exussum12\CoverageChecker\DiffFileLoader($argv[1]);
$phpunit = new exussum12\CoverageChecker\PhpCsLoader($argv[2]);
$coverageCheck = new exussum12\CoverageChecker\CoverageCheck($diff, $phpunit, $matcher);
$matcher = new FileMatchers\EndsWith();
$diff = new DiffFileLoader($argv[1]);
$phpunit = new PhpCsLoader($argv[2]);
$coverageCheck = new CoverageCheck($diff, $phpunit, $matcher);

$lines = $coverageCheck->getCoveredLines();

$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);

if (empty($uncoveredLines)) {
exit(0);
}

print_r($lines['uncoveredLines']);
exit(2);
handleOutput($lines, $minimumPercentCovered);
57 changes: 9 additions & 48 deletions bin/phpmdDiffFilter
Original file line number Diff line number Diff line change
@@ -1,56 +1,17 @@
#!/usr/bin/env php
<?php
$locations = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/../autoload.php'
];
namespace exussum12\CoverageChecker;

$found = false;
include (__DIR__ . "/../functions.php");

foreach ($locations as $file) {
if (file_exists($file)) {
require_once($file);
$found = true;
break;
}
}
setUp();
$minimumPercentCovered = getMinPercent();

if (!$found) {
error_log(
"Can't find the autoload file," .
"please make sure 'composer install' has been run"
);

exit(1);
}

if (!isset($argv[1], $argv[2])) {
error_log(
"Missing arguments, please call with diff and check file"
);
exit(1);
}

if ($argv[1] == "-") {
$argv[1] = "php://stdin";
}
if ($argv[2] == "-") {
$argv[2] = "php://stdin";
}

$matcher = new exussum12\CoverageChecker\FileMatchers\EndsWith();
$diff = new exussum12\CoverageChecker\DiffFileLoader($argv[1]);
$phpunit = new exussum12\CoverageChecker\PhpMdLoader($argv[2]);
$coverageCheck = new exussum12\CoverageChecker\CoverageCheck($diff, $phpunit, $matcher);
$matcher = new FileMatchers\EndsWith();
$diff = new DiffFileLoader($argv[1]);
$phpunit = new PhpMdLoader($argv[2]);
$coverageCheck = new CoverageCheck($diff, $phpunit, $matcher);

$lines = $coverageCheck->getCoveredLines();

$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);

if (empty($uncoveredLines)) {
exit(0);
}

print_r($lines['uncoveredLines']);
exit(2);
handleOutput($lines, $minimumPercentCovered);
70 changes: 9 additions & 61 deletions bin/phpunitDiffFilter
Original file line number Diff line number Diff line change
@@ -1,69 +1,17 @@
#!/usr/bin/env php
<?php
$locations = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/../autoload.php'
];
namespace exussum12\CoverageChecker;

$found = false;
require_once (__DIR__ . "/../functions.php");

foreach ($locations as $file) {
if (file_exists($file)) {
require_once($file);
$found = true;
break;
}
}
setUp();
$minimumPercentCovered = getMinPercent();

if (!$found) {
error_log(
"Can't find the autoload file," .
"please make sure 'composer install' has been run"
);

exit(1);
}

if (!isset($argv[1], $argv[2])) {
error_log(
"Missing arguments, please call with diff and check file"
);
exit(1);
}

if ($argv[1] == "-") {
$argv[1] = "php://stdin";
}
if ($argv[2] == "-") {
$argv[2] = "php://stdin";
}

$minimumPercentCovered = 100;
if (isset($argv[3])) {
$minimumPercentCovered = min($minimumPercentCovered, max(0, $argv[3]));
}


$matcher = new exussum12\CoverageChecker\FileMatchers\EndsWith();
$diff = new exussum12\CoverageChecker\DiffFileLoader($argv[1]);
$phpunit = new exussum12\CoverageChecker\XMLReport($argv[2]);
$coverageCheck = new exussum12\CoverageChecker\CoverageCheck($diff, $phpunit, $matcher);
$matcher = new FileMatchers\EndsWith();
$diff = new DiffFileLoader($argv[1]);
$phpunit = new XMLReport($argv[2]);
$coverageCheck = new CoverageCheck($diff, $phpunit, $matcher);

$lines = $coverageCheck->getCoveredLines();

$coveredLines = count($lines['coveredLines'], COUNT_RECURSIVE);
$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);

if($coveredLines + $uncoveredLines == 0) {
exit(0);
}

$percentCovered = 100 * ($coveredLines / ($coveredLines + $uncoveredLines));
if ($percentCovered >= $minimumPercentCovered) {
exit(0);
}

echo "$percentCovered% Covered, Missed lines " . PHP_EOL;
print_r($lines['uncoveredLines']);
exit(2);
handleOutput($lines, $minimumPercentCovered);
87 changes: 87 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
namespace exussum12\CoverageChecker;

function setUp()
{
findAutoLoader();
checkCallIsCorrect();
adjustForStdIn();
}

function findAutoLoader()
{
$locations = [
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/autoload.php'
];

$found = false;

foreach ($locations as $file) {
if (file_exists($file)) {
require_once($file);
$found = true;
break;
}
}

if (!$found) {
error_log(
"Can't find the autoload file," .
"please make sure 'composer install' has been run"
);

exit(1);
}
}

function checkCallIsCorrect()
{
global $argv;
if (!isset($argv[1], $argv[2])) {
error_log(
"Missing arguments, please call with diff and check file"
);
exit(1);
}
}

function adjustForStdIn()
{
global $argv;
foreach ([1, 2] as $arg) {
if ($argv[$arg] == "-") {
$argv[$arg] = "php://stdin";
}
}
}

function getMinPercent()
{
global $argv;
$minimumPercentCovered = 100;
if (isset($argv[3])) {
$minimumPercentCovered = min($minimumPercentCovered, max(0, $argv[3]));
return $minimumPercentCovered;
}
return $minimumPercentCovered;
}

function handleOutput($lines, $minimumPercentCovered)
{
$coveredLines = count($lines['coveredLines'], COUNT_RECURSIVE);
$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);

if ($coveredLines + $uncoveredLines == 0) {
exit(0);
}

$percentCovered = 100 * ($coveredLines / ($coveredLines + $uncoveredLines));
if ($percentCovered >= $minimumPercentCovered) {
exit(0);
}

echo "$percentCovered% Covered, Missed lines " . PHP_EOL;
print_r($lines['uncoveredLines']);
exit(2);
}
2 changes: 1 addition & 1 deletion src/FileMatchers/EndsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function match($needle, array $haystack)
}

/**
* Month for finding if two strings end in the same way
* Find if two strings end in the same way
* @param $haystack
* @param $needle
* @return bool
Expand Down
60 changes: 60 additions & 0 deletions src/FileMatchers/FileMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace exussum12\CoverageChecker\FileMatchers;

use exussum12\CoverageChecker\FileMatcher;
use exussum12\CoverageChecker\Exceptions\FileNotFound;

/**
* Class FileMapper
* @package exussum12\CoverageChecker\FileMatchers
*/
class FileMapper implements FileMatcher
{
/**
* @var string
*/
protected $originalPath;
/**
* @var string
*/
protected $newPath;

/**
* FileMapper constructor.
* @param string $originalPath
* @param string $newPath
*/
public function __construct($originalPath, $newPath)
{
$this->originalPath = $originalPath;
$this->newPath = $newPath;
}

/**
* {@inheritdoc}
*/
public function match($needle, array $haystack)
{
foreach ($haystack as $file) {
if ($this->checkMapping($file, $needle)) {
return $file;
}
}

throw new FileNotFound();
}

/**
* @param string $file
* @param string $needle
* @return bool
*/
private function checkMapping($file, $needle)
{
return $file == str_replace(
$this->originalPath,
$this->newPath,
$needle
);
}
}
Loading

0 comments on commit 1c860dc

Please sign in to comment.