1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 09:24:03 +00:00
enums/tests/PHPStan/InvokableCases/InvokableCasesTestCase.php
Márk Magyar df3681965c
General code cleanup (#28)
* Add .idea folder to .gitignore

* Remove useless concatenation

* Add phpunit cache directory to .gitignore

* Use iterable type instead of unnecessary checks

* Remove unnecessary null coalescing

If the return value is `null`, it will return `null` anyway. If the method doesn't exist, it will throw an `Error` because of the undefined method.

* Add typehint

* Merge meta properties in one go instead of in a loop

* Simplify control flow

* Use non-deprecated phpstan configuration value

* Add missing types

* Fix style with php-cs-fixer

* Add tests that satisfy the ArrayIterator branch

* Use is() for comparison in in()

* from(int|string), tryFrom(int|string)

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
2025-06-07 01:11:53 +02:00

46 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Testing\PHPStanTestCase;
class InvokableCasesTestCase extends PHPStanTestCase
{
public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/../../../src/PHPStan/InvokableCases/extension.neon'];
}
public function assertStaticallyCallable(string $enum, string $case, $exists = true, $static = true): void
{
$reflectionProvider = $this->createReflectionProvider();
$class = $reflectionProvider->getClass($enum);
if ($exists) {
$this->assertTrue($class->hasMethod($case), sprintf('%s on class %s does not exist', $case, $enum));
$method = $class->getMethod($case, new OutOfClassScope());
if ($static) {
$this->assertTrue($method->isStatic(), sprintf('%s on class %s is not static', $case, $enum));
} else {
$this->assertFalse($method->isStatic(), sprintf('%s on class %s is static', $case, $enum));
}
} else {
$this->assertFalse($class->hasMethod($case), sprintf('%s on class %s exists', $case, $enum));
}
}
public function assertStaticallyCallableType(string $enum, string $case, string $type): void
{
$reflectionProvider = $this->createReflectionProvider();
$class = $reflectionProvider->getClass($enum);
$method = $class->getMethod($case, new OutOfClassScope());
$methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants());
$methodReturnType = $methodVariant->getReturnType();
$this->assertInstanceOf($type, $methodReturnType);
}
}