Skip to content

Commit

Permalink
Use PHPStan (#88)
Browse files Browse the repository at this point in the history
* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Oct 28, 2022
1 parent 68ef457 commit a1e38b2
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/canvas.yaml export-ignore
/phpstan-baseline.neon export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml export-ignore
/sync.sh export-ignore
/testbench.yaml export-ignore
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: analyse

on:
push:
pull_request:

jobs:
analyse:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
os:
- "ubuntu-latest"
php:
- "8.0"
experimental:
- true

name: PHP${{ matrix.php }} on ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, mysql, mysqli, pdo_mysql, bcmath, intl, fileinfo
coverage: none

- name: Install dependencies
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "highest"
composer-options: "--prefer-dist --prefer-stable --no-cache"

- name: Installed dependencies
run: composer show -D

- name: Execute Static Code Analysis
run: vendor/bin/phpstan analyse
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"laravel/pint": "^1.1",
"mockery/mockery": "^1.5.1",
"orchestra/canvas": "^7.0",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.5.10",
"symfony/process": "^6.0.9",
"symfony/yaml": "^6.0.9",
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Instanceof between Illuminate\\\\Http\\\\Response and Illuminate\\\\Http\\\\RedirectResponse will always evaluate to false\\.$#"
count: 1
path: src/TestCase.php
19 changes: 19 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
includes:
- ./phpstan-baseline.neon

parameters:
paths:
- src

# The level 8 is the highest level
level: 6

ignoreErrors:
- '#Unsafe usage of new static#'

excludePaths:
- src/Foundation/Console/TestCommand.php

checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false
30 changes: 16 additions & 14 deletions src/Concerns/HandlesAnnotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ protected function parseTestMethodAnnotations($app, string $name): void
}

if (class_exists(Version::class) && version_compare(Version::id(), '10', '>=')) {
/** @phpstan-ignore-next-line */
$registry = \PHPUnit\Metadata\Annotation\Parser\Registry::getInstance();
} else {
/** @phpstan-ignore-next-line */
$registry = \PHPUnit\Util\Annotation\Registry::getInstance();
}

Collection::make(
rescue(function () use ($registry) {
return $registry->forMethod(static::class, $this->getName(false))->symbolAnnotations();
}, [], false)
)->filter(static function ($actions, $key) use ($name) {
return $key === $name;
})->each(function ($actions) use ($app) {
Collection::make($actions ?? [])
->filter(function ($method) {
return ! \is_null($method) && method_exists($this, $method);
})->each(function ($method) use ($app) {
$this->{$method}($app);
});
});
/** @var array<string, mixed> $annotations */
$annotations = rescue(
fn () => $registry->forMethod(static::class, $this->getName(false))->symbolAnnotations(), [], false
);

Collection::make($annotations)
->filter(fn ($actions, $key) => $key === $name)
->each(function ($actions) use ($app) {
(new Collection($actions ?? []))
->filter(fn ($method) => is_string($method) && method_exists($this, $method))
->each(function ($method) use ($app) {
$this->{$method}($app);
});
});
}
}
13 changes: 11 additions & 2 deletions src/Concerns/Testing.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait Testing
/**
* The Illuminate application instance.
*
* @var \Illuminate\Foundation\Application
* @var \Illuminate\Foundation\Application|null
*/
protected $app;

Expand Down Expand Up @@ -61,7 +61,7 @@ trait Testing
/**
* The exception thrown while running an application destruction callback.
*
* @var \Throwable
* @var \Throwable|null
*/
protected $callbackException;

Expand Down Expand Up @@ -130,6 +130,7 @@ final protected function tearDownTheTestEnvironment(): void
}

if (class_exists(Mockery::class)) {
/** @phpstan-ignore-next-line */
if ($container = Mockery::getContainer()) {
$this->addToAssertionCount($container->mockery_getExpectationCount());
}
Expand Down Expand Up @@ -176,27 +177,33 @@ final protected function setUpTheTestEnvironmentTraits(array $uses): array
{
$this->setUpDatabaseRequirements(function () use ($uses) {
if (isset($uses[RefreshDatabase::class])) {
/** @phpstan-ignore-next-line */
$this->refreshDatabase();
}

if (isset($uses[DatabaseMigrations::class])) {
/** @phpstan-ignore-next-line */
$this->runDatabaseMigrations();
}
});

if (isset($uses[DatabaseTransactions::class])) {
/** @phpstan-ignore-next-line */
$this->beginDatabaseTransaction();
}

if (isset($uses[WithoutMiddleware::class])) {
/** @phpstan-ignore-next-line */
$this->disableMiddlewareForAllTests();
}

if (isset($uses[WithoutEvents::class])) {
/** @phpstan-ignore-next-line */
$this->disableEventsForAllTests();
}

if (isset($uses[WithFaker::class])) {
/** @phpstan-ignore-next-line */
$this->setUpFaker();
}

Expand All @@ -209,6 +216,7 @@ final protected function setUpTheTestEnvironmentTraits(array $uses): array
protected function setUpParallelTestingCallbacks(): void
{
if (class_exists(ParallelTesting::class) && $this instanceof TestCase) {
/** @phpstan-ignore-next-line */
ParallelTesting::callSetUpTestCaseCallbacks($this);
}
}
Expand All @@ -219,6 +227,7 @@ protected function setUpParallelTestingCallbacks(): void
protected function tearDownParallelTestingCallbacks(): void
{
if (class_exists(ParallelTesting::class) && $this instanceof TestCase) {
/** @phpstan-ignore-next-line */
ParallelTesting::callTearDownTestCaseCallbacks($this);
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/Console/Commander.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

/**
* @phpstan-type TConfig array{laravel: string|null, env: array|null, providers: array|null, dont-discover: array|null}
*/
class Commander
{
/**
Expand All @@ -29,9 +32,14 @@ class Commander
/**
* List of configurations.
*
* @var array{laravel: string|null, env: array, providers: array, dont-discover: array}
* @var TConfig
*/
protected $config = [];
protected $config = [
'laravel' => null,
'env' => [],
'providers' => [],
'dont-discover' => [],
];

/**
* Working path.
Expand All @@ -43,7 +51,7 @@ class Commander
/**
* Construct a new Commander.
*
* @param array{laravel: string|null, env: array, providers: array, dont-discover: array} $config
* @param TConfig $config
* @param string $workingPath
*/
public function __construct(array $config, string $workingPath)
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function report(Throwable $e)
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $e
* @return \Illuminate\Http\Response
* @return \Symfony\Component\HttpFoundation\Response|\Illuminate\Http\Response
*/
public function render($request, Throwable $e)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\Illuminate\Foundation\Auth\User>
* @phpstan-type TModel \Illuminate\Foundation\Auth\User
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
*/
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
* @var class-string<TModel>
*/
protected $model = \Illuminate\Foundation\Auth\User::class;

Expand Down
6 changes: 5 additions & 1 deletion src/Foundation/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
use Illuminate\Support\Fluent;
use Symfony\Component\Yaml\Yaml;

/**
* @phpstan-type TConfig array{laravel: string|null, env: array, providers: array, dont-discover: array}
*/
class Config extends Fluent
{
/**
* All of the attributes set on the fluent instance.
*
* @var array{laravel: string|null, env: array, providers: array, dont-discover: array}
* @var TConfig
*/
protected $attributes = [
'laravel' => null,
Expand All @@ -34,6 +37,7 @@ public static function loadFromYaml(string $workingPath, ?string $filename = 'te
$config = $defaults;

if (file_exists("{$workingPath}/{$filename}")) {
/** @var TConfig $config */
$config = Yaml::parseFile("{$workingPath}/{$filename}");

$config['laravel'] = transform(Arr::get($config, 'laravel'), function ($basePath) use ($workingPath) {
Expand Down
1 change: 1 addition & 0 deletions src/Foundation/Console/TestFallbackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ protected function installCollisionDependencies()
*/
protected function findComposer()
{
/** @phpstan-ignore-next-line */
$composerPath = TESTBENCH_WORKING_PATH.'/composer.phar';

if (file_exists($composerPath)) {
Expand Down
1 change: 1 addition & 0 deletions src/Foundation/PackageManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function __construct(Filesystem $files, $basePath, $manifestPath, $testbe
*/
public static function swap($app, $testbench = null)
{
/** @var \Illuminate\Foundation\PackageManifest $base */
$base = $app->make(IlluminatePackageManifest::class);

$app->instance(
Expand Down

0 comments on commit a1e38b2

Please sign in to comment.