Skip to content

Commit

Permalink
Update php-parser (#78)
Browse files Browse the repository at this point in the history
* Update php-parser
  • Loading branch information
exussum12 authored Jun 25, 2024
1 parent fa8d511 commit 0a03507
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 154 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
operating-system: ['ubuntu-latest', 'windows-latest', 'macOS-latest']
php-version: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
php-version: ['8.1', '8.2', '8.3']
name: PHP ${{ matrix.php-version }} Test on ${{ matrix.operating-system }}
steps:
- name: Checkout code
Expand All @@ -23,6 +23,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
tools: composer:v2
coverage: xdebug
- name: Install Composer dependencies
run: composer install --prefer-dist --no-interaction --no-suggest
- name: Install phpcs
Expand Down
6 changes: 2 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#!/bin/sh
set -e

git log origin/master... | grep -q SKIP_BUILD && exit 0

[ -z "$UPDATE_COVERAGE" ] || composer require satooshi/php-coveralls:v1.1.0

composer install
git diff $(git merge-base origin/master HEAD) > diff.txt
[ -z "$UPDATE_COVERAGE" ] || git diff $(git merge-base origin/master HEAD) > diff.txt
phpcs --standard=psr2 src
phpcs --standard=psr2 --ignore=bootstrap.php,fixtures/* tests

Expand All @@ -15,6 +13,6 @@ phpmd tests text cleancode,codesize,controversial,unusedcode --exclude fixtures

./vendor/bin/phpunit

bin/diffFilter --phpunit diff.txt report/coverage.xml
[ -z "$UPDATE_COVERAGE" ] || bin/diffFilter --phpunit diff.txt report/coverage.xml

[ -z "$UPDATE_COVERAGE" ] || php vendor/bin/coveralls -v
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
},
"bin": ["bin/diffFilter"],
"require": {
"php": ">=7.0",
"php": ">=8.1",
"ext-xmlreader": "*",
"ext-json": "*",
"nikic/php-parser": "^3.1||^4.0"
"nikic/php-parser": "^3.1||^4.0||^5.0"
}
}
42 changes: 16 additions & 26 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.4/phpunit.xsd"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuite name="coverageChecker">
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log
type="coverage-html"
target="report"
lowUpperBound="90"
highLowerBound="95"/>
<log type="coverage-clover" target="report/coverage.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd" bootstrap="tests/bootstrap.php" backupGlobals="false" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" cacheDirectory=".phpunit.cache" beStrictAboutCoverageMetadata="true">
<coverage includeUncoveredFiles="true">
<report>
<clover outputFile="report/coverage.xml"/>
<html outputDirectory="report" lowUpperBound="90" highLowerBound="95"/>
</report>
</coverage>
<testsuite name="coverageChecker">
<directory suffix="Test.php">tests</directory>
</testsuite>
<logging/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/FileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FileParser
public function __construct($sourceCode)
{
$this->sourceCode = $sourceCode;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$parser = (new ParserFactory)->createForHostVersion();
$this->parse($parser);
}

Expand Down Expand Up @@ -56,7 +56,7 @@ protected function getCodeLimits(Node $node): CodeLimits
$startLine = $node->getAttribute('startLine');
$endLine = $node->getAttribute('endLine');
if ($node->getDocComment()) {
$startLine = $node->getDocComment()->getLine();
$startLine = $node->getDocComment()->getStartLine();
}

return new CodeLimits($startLine, $endLine);
Expand Down
23 changes: 14 additions & 9 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function handleOutput(array $lines, float $minimumPercentCovered, Output $output

if ($coveredLines + $uncoveredLines == 0) {
error_log('No lines found!');

$output->output(
$lines['uncoveredLines'],
100,
Expand Down Expand Up @@ -126,14 +126,19 @@ function calculateLines(array $lines)

function addExceptionHandler()
{
set_exception_handler(
function ($exception) {
// @codeCoverageIgnoreStart
error_log($exception->getMessage());
exit($exception->getCode());
// @codeCoverageIgnoreEnd
}
);
if ((
!defined('PHPUNIT_COMPOSER_INSTALL') &&
!defined('__PHPUNIT_PHAR__')
)) {
set_exception_handler(
function ($exception) {
// @codeCoverageIgnoreStart
error_log($exception->getMessage());
exit($exception->getCode());
// @codeCoverageIgnoreEnd
}
);
}
}

function getFileChecker(
Expand Down
5 changes: 2 additions & 3 deletions tests/ArgParserTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace exussum12\CoverageChecker\tests;

use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\TestCase;
use exussum12\CoverageChecker\ArgParser;
use exussum12\CoverageChecker\Exceptions\ArgumentNotFound;
Expand All @@ -9,9 +10,7 @@ class ArgParserTest extends TestCase
{
protected $parser;

/**
* @before
*/
#[Before]
public function setUpTest()
{
$args = [
Expand Down
108 changes: 49 additions & 59 deletions tests/CoverageCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@ public function testCoverage()
]);

$xmlReport->method('getErrorsOnLine')
->will(
$this->returnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);

if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return $this->errorMessage;
}
if ($file == '/full/path/to/testFile2.php' && $line == 4) {
return $this->errorMessage;
}

return [];
->willReturnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);

if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return $this->errorMessage;
}
if ($file == '/full/path/to/testFile2.php' && $line == 4) {
return $this->errorMessage;
}
)

return [];
}
);

$matcher = new FileMatchers\EndsWith;
Expand Down Expand Up @@ -87,19 +85,17 @@ public function testCoverageFailed()
->willReturn(null);

$xmlReport->method('getErrorsOnLine')
->will(
$this->returnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);
->willReturnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);

if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return $this->errorMessage;
}

return [];
if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return $this->errorMessage;
}
)

return [];
}
);

$matcher = new FileMatchers\EndsWith;
Expand Down Expand Up @@ -141,19 +137,17 @@ public function testAddingAllUnknownsCovered()
->willReturn(true);

$xmlReport->method('getErrorsOnLine')
->will(
$this->returnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);

if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return $this->errorMessage;
}
->willReturnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);

return [];
if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return $this->errorMessage;
}
)

return [];
}
);

$matcher = new FileMatchers\EndsWith;
Expand Down Expand Up @@ -196,19 +190,17 @@ public function testAddingAllUnknownsUnCovered()
->willReturn(false);

$xmlReport->method('getErrorsOnLine')
->will(
$this->returnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);

if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return $this->errorMessage;
}
->willReturnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);

return [];
if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return $this->errorMessage;
}
)

return [];
}
);

$matcher = new FileMatchers\EndsWith;
Expand Down Expand Up @@ -254,19 +246,17 @@ public function testCoverageForContextLines()
->willReturn(false);

$xmlReport->method('getErrorsOnLine')
->will(
$this->returnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);
->willReturnCallback(
function () {
$file = func_get_arg(0);
$line = func_get_arg(1);

if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return null;
}

return [];
if ($file == '/full/path/to/testFile1.php' && $line == 2) {
return null;
}
)

return [];
}
);

$matcher = new FileMatchers\EndsWith;
Expand Down
9 changes: 4 additions & 5 deletions tests/DiffFileLoadOldVersionTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php
namespace exussum12\CoverageChecker\tests;

use exussum12\CoverageChecker\DiffFileLoaderOldVersion;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use exussum12\CoverageChecker\DiffFileLoaderOldVersion;

class DiffFileLoadOldVersionTest extends TestCase
{
/**
* @dataProvider getResults
*/
#[DataProvider('getResults')]
public function testDiffResultsMatch($file, $expected)
{
$changed = $this->getChangedLines($file);

$this->assertEquals($expected, $changed);
}

public function getResults()
public static function getResults()
{
return [
'newFile' => [
Expand Down
7 changes: 3 additions & 4 deletions tests/DiffFileLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
namespace exussum12\CoverageChecker\tests;

use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use exussum12\CoverageChecker\DiffFileLoader;
use exussum12\CoverageChecker\DiffFileState;
use exussum12\CoverageChecker\DiffLineHandle\ContextLine;

class DiffFileLoadTest extends TestCase
{
/**
* @dataProvider getResults
*/
#[DataProvider('getResults')]
public function testDiffResultsMatch($file, $expected)
{
$changed = $this->getChangedLines($file);
Expand All @@ -26,7 +25,7 @@ public function testNonExistantFile()
$this->getChangedLines('ufhbubfusdf');
}

public function getResults()
public static function getResults()
{
return [
'newFile' => [
Expand Down
5 changes: 2 additions & 3 deletions tests/Loaders/CheckstyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
namespace exussum12\CoverageChecker\tests\Loaders;

use exussum12\CoverageChecker\Loaders\Checkstyle;
use PHPUnit\Framework\Attributes\Before;

class CheckstyleTest extends PhanTextTest
{
/** @var Checkstyle */
protected $phan;
protected $prefix = '';

/**
* @before
*/
#[Before]
protected function setUpTest()
{
$this->phan = new Checkstyle(__DIR__ . '/../fixtures/checkstyle.xml');
Expand Down
Loading

0 comments on commit 0a03507

Please sign in to comment.