Skip to content

Commit

Permalink
Merge pull request #47 from exussum12/v1-prep
Browse files Browse the repository at this point in the history
Start of v1
  • Loading branch information
exussum12 authored Dec 18, 2018
2 parents 3623b9d + d86c446 commit 4fe88f8
Show file tree
Hide file tree
Showing 83 changed files with 538 additions and 525 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ cache:
- $HOME/.cache/composer/files
matrix:
include:
- php: 5.6
- php: 7.0
- php: 7.1
env: UPDATE_COVERAGE=1
- php: 7.2
- php: nightly
- php: hhvm
allow_failures:
- php: hhvm
env: UPDATE_COVERAGE=1
- php: 7.3
- php: nightly
fast_finish: true
before_script:
Expand Down
17 changes: 17 additions & 0 deletions GitHooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash -e

# This is a PSR-2 checking example for just the code about to be committed

# Get the changes
files=$(mktemp)
diff=$(mktemp)

git diff --cached --name-only --diff-filter=ACMR -- "*.php" > ${files}
git diff --cached > ${diff}

# Run the phpcs report
phpcs=$(mktemp)
./vendor/bin/phpcs --file-list=${files} --parallel=2 --standard=psr2 --report=json > ${phpcs} || true

check for differences
./vendor/bin/diffFilter --phpcs diff.txt phpcs.json
17 changes: 17 additions & 0 deletions GitHooks/pre-receive
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# This is a PSR-2 checking example for just the code about to be committed

# Get the changes
files=$(mktemp)
diff=$(mktemp)

git diff --name-only --diff-filter=ACMR -- "*.php" $1...$2 > ${files}
git diff $1...$2 > ${diff}

# Run the phpcs report
phpcs=$(mktemp)
./vendor/bin/phpcs --file-list=${files} --parallel=2 --standard=psr2 --report=json > ${phpcs} || true

check for differences
./vendor/bin/diffFilter --phpcs diff.txt phpcs.json
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Coverage checker allows new standards to be implemented incrementally, by only e

Tools like phpcs and phpmd are an all or nothing approach, coverage checker allows this to work with the diff i.e. enforce all of the pull request / change request.

This is sometimes called "Baselining"

Also working with PHPunit to allow, for example 90% of new/edited code to be covered. which will increase the overall coverage over time.

# Installing
Expand Down Expand Up @@ -54,6 +56,12 @@ diffFilter will exit with a `0` status if the changed code passes the minimum co
## Extended guide
A more in depth guide can be [found on the wiki](https://github.com/exussum12/coverageChecker/wiki) also some tips for speeding up the build.

## Installing as a git hook

There are 2 examples hooks in the GitHooks directory, if you symlink to these diffFilter will run locally.

pre-commit is before the commit happens
pre-receive will prevent you pushing

# Full list of available diff filters

Expand Down
6 changes: 0 additions & 6 deletions bin/phpcsDiffFilter

This file was deleted.

6 changes: 0 additions & 6 deletions bin/phpmdDiffFilter

This file was deleted.

6 changes: 0 additions & 6 deletions bin/phpunitDiffFilter

This file was deleted.

8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "exussum12/coverage-checker",
"description": "Allows checking the code coverage of a single pull request",
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^6.5"
},
"license": "MIT",
"authors": [
Expand All @@ -17,9 +17,11 @@
"exussum12\\CoverageChecker\\tests\\": "tests/"
}
},
"bin": ["bin/phpunitDiffFilter","bin/phpcsDiffFilter", "bin/phpmdDiffFilter", "bin/diffFilter"],
"bin": ["bin/diffFilter"],
"require": {
"php": ">=5.5",
"php": ">=7.0",
"ext-xmlreader": "*",
"ext-json": "*",
"nikic/php-parser": "^3.1||^4.0"
}
}
3 changes: 1 addition & 2 deletions examples/phpcsEnforce.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ set -e

# Get the diff (changes since the branch was created
git diff origin/master... > diff.txt
git diff origin/master... --name-only -- '*.php' > files.txt
git diff origin/master... --name-only --diff-filter=ACMR -- '*.php' > files.txt


# Old versions of phpcs will need to use the syntax commented out.
# Note the || true is important, Without this phpcs failing will fail the build!
# ./vendor/bin/phpcs --standard=psr2 src > phpcs.json || true

./vendor/bin/phpcs --file-list=files.txt --parallel=2 --standard=psr2 --report=json > phpcs.json || true

Expand Down
28 changes: 15 additions & 13 deletions src/ArgParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace exussum12\CoverageChecker;

use exussum12\CoverageChecker\Exceptions\ArgumentNotFound;

class ArgParser
{
protected $args;
Expand All @@ -10,27 +12,31 @@ public function __construct(array $args)
$this->args = $args;
}

public function getArg($name)
/**
* @throws ArgumentNotFound
*/
public function getArg(string $name): string
{
if (is_numeric($name)) {
$name = (int) $name;
return $this->numericArg($name);
}

return $this->letterArg($name);
}

protected function numericArg($position)
protected function numericArg(int $position): string
{
foreach ($this->args as $arg) {
if ($arg{0} != '-' && $position-- == 0) {
return $arg;
}
}

return null;
throw new ArgumentNotFound();
}

protected function letterArg($name)
protected function letterArg($name): string
{
$name = $this->getAdjustedArg($name);
foreach ($this->args as $arg) {
Expand All @@ -41,24 +47,20 @@ protected function letterArg($name)
}
}

return false;
throw new ArgumentNotFound();
}

protected function splitArg($arg)
protected function splitArg(string $arg): array
{
$value = true;
if (strpos($arg, '=')) {
$value = '1';
if (strpos($arg, '=') > 0) {
list($arg, $value) = explode('=', $arg, 2);
}

return array($value, $arg);
}

/**
* @param string $name
* @return string
*/
protected function getAdjustedArg($name)
protected function getAdjustedArg(string $name): string
{
$name = strlen($name) == 1 ?
'-' . $name :
Expand Down
6 changes: 3 additions & 3 deletions src/CodeLimits.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ class CodeLimits
protected $startLine;
protected $endLine;

public function __construct($startLine, $endLine)
public function __construct(int $startLine, int $endLine)
{
$this->startLine = $startLine;
$this->endLine = $endLine;
}

public function getStartLine()
public function getStartLine(): int
{
return $this->startLine;
}

public function getEndLine()
public function getEndLine(): int
{
return $this->endLine;
}
Expand Down
40 changes: 11 additions & 29 deletions src/CoverageCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class CoverageCheck
* For example if the checker is phpunit, this class filters the phpunit
* output by the diff of the pull request. giving only the common lines in
* each
*
* @param DiffFileLoader $diff
* @param FileChecker $fileChecker
* @param FileMatcher $matcher
*/
public function __construct(
DiffFileLoader $diff,
Expand All @@ -63,9 +59,8 @@ public function __construct(

/**
* array of uncoveredLines and coveredLines
* @return array
*/
public function getCoveredLines()
public function getCoveredLines(): array
{
$this->getDiff();

Expand All @@ -76,7 +71,7 @@ public function getCoveredLines()
$diffFiles = array_keys($this->cache->diff);
foreach ($diffFiles as $file) {
$matchedFile = $this->findFile($file, $coveredFiles);
if ($matchedFile !== false) {
if ($matchedFile !== '') {
$this->matchLines($file, $matchedFile);
}
}
Expand All @@ -87,12 +82,7 @@ public function getCoveredLines()
];
}

/**
* @param string $file the filename containing the uncovered line
* @param int $line the number of the uncovered line
* @param array $message a list of messages showing why its uncovered
*/
protected function addUnCoveredLine($file, $line, $message)
protected function addUnCoveredLine(string $file, int $line, array $message)
{
if (!isset($this->uncoveredLines[$file])) {
$this->uncoveredLines[$file] = [];
Expand All @@ -101,11 +91,7 @@ protected function addUnCoveredLine($file, $line, $message)
$this->uncoveredLines[$file][$line] = $message;
}

/**
* @param string $file the filename containing the covered line
* @param int $line the number of the covered line
*/
protected function addCoveredLine($file, $line)
protected function addCoveredLine(string $file, int $line)
{
if (!isset($this->coveredLines[$file])) {
$this->coveredLines[$file] = [];
Expand All @@ -114,11 +100,7 @@ protected function addCoveredLine($file, $line)
$this->coveredLines[$file][] = $line;
}

/**
* @param string $fileName the file name in the diff
* @param string $matchedFile the file name of the matched file
*/
protected function matchLines($fileName, $matchedFile)
protected function matchLines(string $fileName, string $matchedFile)
{
foreach ($this->cache->diff[$fileName] as $line) {
$messages = $this->fileChecker->getErrorsOnLine($matchedFile, $line);
Expand All @@ -141,21 +123,21 @@ protected function matchLines($fileName, $matchedFile)
}
}

protected function addCoveredFile($file)
protected function addCoveredFile(string $file)
{
foreach ($this->cache->diff[$file] as $line) {
$this->addCoveredLine($file, $line);
}
}

protected function addUnCoveredFile($file)
protected function addUnCoveredFile(string $file)
{
foreach ($this->cache->diff[$file] as $line) {
$this->addUnCoveredLine($file, $line, ['No Cover']);
}
}

protected function getDiff()
protected function getDiff(): array
{
if (empty($this->cache->diff)) {
$this->cache->diff = $this->diff->getChangedLines();
Expand All @@ -164,7 +146,7 @@ protected function getDiff()
return $this->cache->diff;
}

protected function handleFileNotFound($file)
protected function handleFileNotFound(string $file)
{
$unMatchedFile = $this->fileChecker->handleNotFoundFile();

Expand All @@ -177,13 +159,13 @@ protected function handleFileNotFound($file)
}
}

protected function findFile($file, $coveredFiles)
protected function findFile(string $file, array $coveredFiles): string
{
try {
return $this->matcher->match($file, $coveredFiles);
} catch (Exceptions\FileNotFound $e) {
$this->handleFileNotFound($file);
return false;
return '';
}
}
}
10 changes: 5 additions & 5 deletions src/DiffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct($fileName)
$this->diff = new DiffFileState();
}

public function getChangedLines()
public function getChangedLines(): array
{
if ((
!is_readable($this->fileLocation) &&
Expand All @@ -45,19 +45,19 @@ public function getChangedLines()
return $this->diff->getChangedLines();
}

private function getLineHandle($line)
private function getLineHandle(string $line): DiffLineHandle
{
foreach ($this->diffLines as $lineType) {
$lineType = $this->getClass($lineType);
if ($lineType->isValid($line)) {
return $lineType;
}
}
//not found, Class it as context
// the line doesn't have a special meaning, its probably context
return $this->getClass(DiffLineHandle\ContextLine::class);
}

private function getClass($className)
private function getClass(string $className): DiffLineHandle
{
if (!isset($this->handles[$this->getFileHandleName($className)])) {
$this->handles[
Expand All @@ -68,7 +68,7 @@ private function getClass($className)
return $this->handles[$this->getFileHandleName($className)];
}

private function getFileHandleName($namespace)
private function getFileHandleName(string $namespace): string
{
$namespace = explode('\\', $namespace);
return end($namespace);
Expand Down
Loading

0 comments on commit 4fe88f8

Please sign in to comment.