Skip to content

Commit

Permalink
[minor] add static code analysis with phpstan (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Feb 14, 2022
1 parent 38cf18f commit 9c3b326
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 8 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,25 @@ jobs:
with:
php: 7.2
version: 3.4

sca:
name: Static Code Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: none

- name: Install Dependencies
uses: ramsey/composer-install@v1

- name: Install PHPUnit
run: vendor/bin/simple-phpunit install

- name: Run PHPStan
run: vendor/bin/phpstan --error-format=github
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"symfony/polyfill-php81": "^1.23"
},
"require-dev": {
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-phpunit": "^1.0",
"symfony/phpunit-bridge": "^5.3"
},
"config": {
Expand Down
37 changes: 37 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$assertion of static method Zenstruck\\\\Assert\\:\\:run\\(\\) expects callable\\(\\)\\: void, Closure\\(\\)\\: 'value' given\\.$#"
count: 1
path: tests/AssertTest.php

-
message: "#^Parameter \\#2 \\$haystack of class Zenstruck\\\\Assert\\\\Assertion\\\\ContainsAssertion constructor expects bool\\|float\\|int\\|iterable\\|string, stdClass given\\.$#"
count: 1
path: tests/Assertion/ContainsAssertionTest.php

-
message: "#^Parameter \\#2 \\$haystack of class Zenstruck\\\\Assert\\\\Assertion\\\\CountAssertion constructor expects Countable\\|iterable, int given\\.$#"
count: 1
path: tests/Assertion/CountAssertionTest.php

-
message: "#^Parameter \\#2 \\$expectedException of class Zenstruck\\\\Assert\\\\Assertion\\\\ThrowsAssertion constructor expects \\(callable\\(Throwable\\)\\: void\\)\\|string, Closure\\(string\\)\\: void given\\.$#"
count: 1
path: tests/Assertion/ThrowsAssertionTest.php

-
message: "#^Parameter \\#2 \\$expectedException of class Zenstruck\\\\Assert\\\\Assertion\\\\ThrowsAssertion constructor expects \\(callable\\(Throwable\\)\\: void\\)\\|string, array\\{\\} given\\.$#"
count: 1
path: tests/Assertion/ThrowsAssertionTest.php

-
message: "#^Parameter \\#1 \\$expectedException of method Zenstruck\\\\Assert\\\\Expectation\\:\\:throws\\(\\) expects \\(callable\\(Throwable\\)\\: void\\)\\|string, Closure\\(Exception\\)\\: void given\\.$#"
count: 3
path: tests/ExpectationTest.php

-
message: "#^Parameter \\#1 \\$expectedException of method Zenstruck\\\\Assert\\\\Expectation\\:\\:throws\\(\\) expects \\(callable\\(Throwable\\)\\: void\\)\\|string, Closure\\(RuntimeException\\)\\: void given\\.$#"
count: 3
path: tests/ExpectationTest.php

13 changes: 13 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
includes:
- phpstan-baseline.neon
- vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
level: 8
paths:
- src
- tests
treatPhpDocTypesAsCertain: false
checkMissingIterableValueType: false
bootstrapFiles:
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
8 changes: 6 additions & 2 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ public static function false(bool $expression, string $message, array $context =
/**
* Trigger a generic assertion failure.
*
* @return never-return
* @return never
*
* @throws \Throwable
*/
public static function fail(string $message, array $context = []): void
{
self::run(new AssertionFailed($message, $context));
self::run($e = new AssertionFailed($message, $context));

throw $e;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Assert/Assertion/ComparisonAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ final class ComparisonAssertion extends EvaluableAssertion
/** @var string */
private $comparison;

/**
* @param mixed $actual
* @param mixed $expected
*/
private function __construct($actual, $expected, string $comparison, ?string $message = null, array $context = [])
{
$this->actual = $actual;
Expand Down
10 changes: 8 additions & 2 deletions src/Assert/AssertionFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function __construct(string $message, array $context = [], ?\Throwable $p
parent::__construct(self::createMessage($message, $context), 0, $previous);
}

/**
* @return never
*/
public function __invoke(): void
{
throw $this;
Expand All @@ -27,7 +30,7 @@ public function __invoke(): void
/**
* Create and throw.
*
* @psalm-return no-return
* @return never
*/
public static function throw(string $message, array $context = [], ?\Throwable $previous = null): void
{
Expand Down Expand Up @@ -55,6 +58,9 @@ private static function createMessage(string $template, array $context): string
return \strtr($template, self::normalizeContext($context));
}

/**
* @param mixed $value
*/
private static function normalizeContextValue($value): string
{
if (\is_object($value)) {
Expand All @@ -69,7 +75,7 @@ private static function normalizeContextValue($value): string
return \sprintf('(%s)', \var_export($value, true));
}

$value = \preg_replace('/\s+/', ' ', $value);
$value = (string) \preg_replace('/\s+/', ' ', (string) $value);

if (\mb_strlen($value) <= self::MAX_SHORT_LENGTH) {
return $value;
Expand Down
16 changes: 12 additions & 4 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function true_failure(): void
Assert::true(false, 'message1');
$this->assertSame('message1', $this->handler->lastFailureMessage());

Assert::true(\is_string(5), 'message2 with %s', ['context']);
Assert::true(false, 'message2 with %s', ['context']);
$this->assertSame('message2 with context', $this->handler->lastFailureMessage());

$this->assertSame(0, $this->handler->successCount());
Expand All @@ -130,7 +130,7 @@ public function false_success(): void
$this->assertSame(0, $this->handler->failureCount());

Assert::false(false, 'message1');
Assert::false(\is_string(5), 'message2');
Assert::false(false, 'message2');

$this->assertSame(2, $this->handler->successCount());
$this->assertSame(0, $this->handler->failureCount());
Expand Down Expand Up @@ -162,10 +162,18 @@ public function generic_fail(): void
$this->assertSame(0, $this->handler->successCount());
$this->assertSame(0, $this->handler->failureCount());

Assert::fail('message1');
try {
Assert::fail('message1');
} catch (AssertionFailed $e) {
}

$this->assertSame('message1', $this->handler->lastFailureMessage());

Assert::fail('message2 with %s', ['context']);
try {
Assert::fail('message2 with %s', ['context']);
} catch (AssertionFailed $e) {
}

$this->assertSame('message2 with context', $this->handler->lastFailureMessage());

$this->assertSame(0, $this->handler->successCount());
Expand Down
1 change: 1 addition & 0 deletions tests/Fixture/CountableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
final class CountableObject implements \Countable
{
/** @var int */
private $count;

public function __construct(int $count)
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixture/IterableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @implements \IteratorAggregate<int|string, mixed>
*/
final class IterableObject implements \IteratorAggregate
{
/** @var array */
private $value;

public function __construct(array $value)
Expand Down
1 change: 1 addition & 0 deletions tests/Fixture/NegatableAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
final class NegatableAssertion implements Negatable
{
/** @var bool */
private $fail;

public function __construct(bool $fail)
Expand Down

0 comments on commit 9c3b326

Please sign in to comment.