1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 17:34:02 +00:00

Added "Invokable Cases" PHPStan extension (#13)

* Added "Invokable Cases" PHPStan extension

This includes a PHPStan extension to add support for `InvokableCases`
so that static analysis tools can understand the callable methods and
their return types.

The extension has been added in a way that will allow multiple extensions
in the future if required, with a single include file that will import
all extensions, or the option to include only specific extensions.

* Added "Invokable Cases" PHPStan extension testing
This commit is contained in:
Samuel Levy 2022-08-25 08:27:44 +10:00 committed by GitHub
parent 7e17b84451
commit 373a86a16e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 382 additions and 0 deletions

View file

@ -0,0 +1,99 @@
<?php
use ArchTech\Enums\Tests\PHPStan\InvokableCases\InvokableCasesTestCase;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
uses(InvokableCasesTestCase::class);
it('correctly identifies allowable static method calls for invokable pure enum', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Role::class;
// Base enum
$this->assertStaticallyCallable($class, 'cases');
$this->assertStaticallyCallable($class, 'from', false);
$this->assertStaticallyCallable($class, 'tryFrom', false);
// Defined methods
$this->assertStaticallyCallable($class, 'administrator');
$this->assertStaticallyCallable($class, 'isManager', true, false);
// Cases
$this->assertStaticallyCallable($class, 'admin');
$this->assertStaticallyCallable($class, 'manager');
$this->assertStaticallyCallable($class, 'staff');
// Missing Case
$this->assertStaticallyCallable($class, 'customer', false);
});
it('correctly identifies allowable static method calls for invokable int backed enum', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Status::class;
// Base enum
$this->assertStaticallyCallable($class, 'cases');
$this->assertStaticallyCallable($class, 'from');
$this->assertStaticallyCallable($class, 'tryFrom');
// Defined methods
$this->assertStaticallyCallable($class, 'initial');
$this->assertStaticallyCallable($class, 'isStarted', true, false);
// Cases
$this->assertStaticallyCallable($class, 'created');
$this->assertStaticallyCallable($class, 'running');
$this->assertStaticallyCallable($class, 'done');
// Missing Case
$this->assertStaticallyCallable($class, 'failed', false);
});
it('correctly identifies allowable static method calls for invokable string backed enum', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Suits::class;
// Base enum
$this->assertStaticallyCallable($class, 'cases');
$this->assertStaticallyCallable($class, 'from');
$this->assertStaticallyCallable($class, 'tryFrom');
// Defined methods
$this->assertStaticallyCallable($class, 'valuable');
$this->assertStaticallyCallable($class, 'isRed', true, false);
// Cases
$this->assertStaticallyCallable($class, 'clubs');
$this->assertStaticallyCallable($class, 'diamonds');
$this->assertStaticallyCallable($class, 'hearts');
$this->assertStaticallyCallable($class, 'spades');
// Missing Case
$this->assertStaticallyCallable($class, 'joker', false);
});
it('correctly identifies types for invoked pure enum cases', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Role::class;
// Cases
$this->assertStaticallyCallableType($class, 'admin', StringType::class);
$this->assertStaticallyCallableType($class, 'manager', StringType::class);
$this->assertStaticallyCallableType($class, 'staff', StringType::class);
});
it('correctly identifies types for invoked int backed enum cases', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Status::class;
// Cases
$this->assertStaticallyCallableType($class, 'created', IntegerType::class);
$this->assertStaticallyCallableType($class, 'running', IntegerType::class);
$this->assertStaticallyCallableType($class, 'done', IntegerType::class);
});
it('correctly identifies types for invoked string backed enum cases', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Suits::class;
// Cases
$this->assertStaticallyCallableType($class, 'clubs', StringType::class);
$this->assertStaticallyCallableType($class, 'diamonds', StringType::class);
$this->assertStaticallyCallableType($class, 'hearts', StringType::class);
$this->assertStaticallyCallableType($class, 'spades', StringType::class);
});