Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for PHP 8.1 compatibility, add github workflow #2

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events
push:
pull_request:

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# Composer config validation
composer:
name: Composer config validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Validate composer.json
run: composer validate --strict

# PHP syntax validation
php:
name: PHP syntax validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check PHP syntax of config/phinx.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the action is not correct

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name has been fixed

run: php -l src/ tests/

phpunit:
name: PHPUnit tests
runs-on: ubuntu-latest
strategy:
matrix:
php_version: [7.4, 8.0, 8.1, 8.2]
steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php_version }}
- run: vendor/bin/phpunit

# phpstan for several php versions
phpstan:
runs-on: ubuntu-latest
strategy:
matrix:
php_version: [7.4, 8.0, 8.1, 8.2]
steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php_version }}
- name: PHPStan Static Analysis
uses: php-actions/phpstan@v3
with:
php_version: ${{ matrix.php_version }}
configuration: phpstan.neon
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
composer.lock
/.phpunit.result.cache
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"minimum-stability": "stable",
"require": {
"php": "^5.5|7.*"
"php": "^7.4|^8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -18,6 +18,6 @@
}
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^9.5.13"
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 9
paths:
- src/
- tests/
39 changes: 19 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
bootstrap="vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
>
<testsuites>
<testsuite name="PHP backtrace tests">
<directory suffix="Test.php">tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="PHP backtrace tests">
<directory suffix="Test.php">tests/</directory>
</testsuite>
</testsuites>
</phpunit>
18 changes: 11 additions & 7 deletions src/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Backtrace
const MIN_OFFSET = 1;

/**
* @var array
* @var array<int, array<mixed>>
*/
public $backtrace;

Expand Down Expand Up @@ -44,7 +44,7 @@ public function __construct($offset = null, $fileRoot = null)
* Remove the given number of steps from the backtrace. The minimum number
* of steps will always be removed.
* @param int|null $offset
* @return array
* @return array<int, array<mixed>>
*/
private function sliceBacktrace($offset)
{
Expand All @@ -66,6 +66,7 @@ protected static function normalizeNumber($offset)
if (is_string($offset) && !is_numeric($offset)) {
return null;
}
/** @var int $offset */
return (int)$offset;
}

Expand All @@ -90,6 +91,7 @@ private function calculateOffset($offset)
/**
* Remove the root path from the file paths of the backtrace.
* @param string $fileRoot
* @return void
*/
private function removeFileRootFromPath($fileRoot)
{
Expand All @@ -109,7 +111,7 @@ private function removeFileRootFromPath($fileRoot)
}

/**
* @param array $step
* @param array<mixed> $step
* @return string
*/
protected function funcString(array $step)
Expand All @@ -123,15 +125,17 @@ protected function funcString(array $step)
$this->funcArgsString($step)
);
}

return sprintf(
'%s(%s)',
/** @phpstan-ignore-next-line */
$step['function'],
$this->funcArgsString($step)
);
}

/**
* @param array $step
* @param array<mixed> $step
* @return string
*/
protected function funcArgsString(array $step)
Expand Down Expand Up @@ -171,7 +175,7 @@ public function countSteps()
/**
* @param int|null $pos
* @param string|null $attribute
* @return array|string|int|object|null
* @return array<mixed>|string|int|object|null|mixed
*/
public function getStep($pos = null, $attribute = null)
{
Expand All @@ -191,10 +195,10 @@ public function getStep($pos = null, $attribute = null)
/**
* Get the last step of the backtrace. Most likely where the error occurred.
* @param string|null $attribute
* @return array|int|object|string|null
* @return array<mixed>|int|object|string|null|mixed
*/
public function lastStep($attribute = null)
{
return $this->getStep(0, $attribute);
}
}
}
5 changes: 3 additions & 2 deletions src/ClassicBacktrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function classicString($offset = null, $fileRoot = null)
{
$offset = (int) static::normalizeNumber($offset);
$offset++; //this method adds to offset
$trace = new static($offset, $fileRoot);
$trace = new self($offset, $fileRoot);
return sprintf('%s', $trace->getClassicString());
}

Expand All @@ -41,6 +41,7 @@ public static function classicString($offset = null, $fileRoot = null)
public function getClassicString()
{
$result = [];
/** @var array<mixed> $step */
foreach ($this->backtrace as $pos => $step) {
$result[] = sprintf(
'#%u%s %s%s',
Expand All @@ -54,7 +55,7 @@ public function getClassicString()
}

/**
* @param array $step
* @param array<mixed> $step
* @return string
*/
private function fileAndLine(array $step)
Expand Down
35 changes: 20 additions & 15 deletions tests/BacktraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class BacktraceTest extends TestCase
{
/**
* Test the default constructor settings.
* @return void
*/
public function testConstructorDefaults()
{
$trace = new Backtrace();
static::assertSame(11, $trace->countSteps());
static::assertSame(12, $trace->countSteps());
static::assertSame(__CLASS__, $trace->getStep(0, 'class'));
static::assertSame(__FUNCTION__, $trace->getStep(0, 'function'));
static::assertSame(__CLASS__, $trace->lastStep('class'));
Expand All @@ -27,27 +28,28 @@ public function testConstructorDefaults()

/**
* Test whether an offset actually removes a trace step.
* @return void
*/
public function testOffset()
{
$trace = new Backtrace(1);
static::assertSame(10, $trace->countSteps());
static::assertSame('ReflectionMethod', $trace->getStep(0, 'class'));
static::assertSame('invokeArgs', $trace->getStep(0, 'function'));
static::assertSame(11, $trace->countSteps());
static::assertSame('PHPUnit\Framework\TestCase', $trace->getStep(0, 'class'));
static::assertSame('runTest', $trace->getStep(0, 'function'));
}

/**
* @return array
* @return array<mixed>
*/
public static function provideInvalidOffsets()
{
return [
[-1, 12],
[null, 12],
['abc', 12],
[0.7, 12],
[[], 12],
[new \stdClass(), 12]
[-1, 13],
[null, 13],
['abc', 13],
[0.7, 13],
[[], 13],
[new \stdClass(), 13]
];
}

Expand All @@ -56,6 +58,7 @@ public static function provideInvalidOffsets()
* @param int $offset
* @param int $expectedSteps
* @dataProvider provideInvalidOffsets
* @return void
*/
public function testInvalidOffsets($offset, $expectedSteps)
{
Expand All @@ -65,6 +68,7 @@ public function testInvalidOffsets($offset, $expectedSteps)

/**
* Test whether removing a file root works.
* @return void
*/
public function testFileRoot()
{
Expand All @@ -77,14 +81,15 @@ public function testFileRoot()

/**
* Test getting a single test step.
* @return void
*/
public function testGetStepPos()
{
$trace = new Backtrace();
//whithout an attribute, the whole trace step will be returned.
static::assertInternalType('array', $trace->getStep(0));
//The first trace step has no file, because it's a reflection
static::assertNull($trace->getStep(0, 'file'));
//Without an attribute, the whole trace step will be returned.
static::assertIsArray($trace->getStep(0));
//The first trace step a file
static::assertNotNull($trace->getStep(0, 'file'));
//There is no pos -1.
static::assertNull($trace->getStep(-1, 'function'));
//There is no pos after the last.
Expand Down
14 changes: 7 additions & 7 deletions tests/ClassicBacktraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ClassicBacktraceTest extends TestCase
{
/**
* Test the inheritance chain.
* @return void
*/
public function testInheritance()
{
Expand All @@ -24,19 +25,18 @@ public function testInheritance()

/**
* Test classic trace string creation.
* @return void
*/
public function testClassicString()
{
$trace1 = ClassicBacktrace::classicString(null, '/app');
static::assertInternalType('string', $trace1);
static::assertIsString($trace1);
$steps = explode(PHP_EOL, $trace1);
static::assertInternalType('array', $steps);
static::assertCount(11, $steps, $trace1);
static::assertIsArray($steps);
static::assertCount(12, $steps, $trace1);
//Reflection has no file or line, just a class and its method.
static::assertSame(
'#0 ' . __CLASS__ . '->' . __FUNCTION__ . '()',
$steps[0]
);
$regexp = sprintf("/^#0\s+%s->%s\(\) called at \[.+\]$/", preg_quote(__CLASS__), preg_quote(__FUNCTION__));
static::assertMatchesRegularExpression($regexp, $steps[0]);
$trace2 = (string)(new ClassicBacktrace(null, '/app'));
static::assertSame($trace1, $trace2);
}
Expand Down
Loading