2021-11-15 18:27:19 +01:00
|
|
|
<?php
|
2021-12-15 04:42:37 +01:00
|
|
|
|
2021-11-15 18:27:19 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psalm\Tests\Internal\Codebase;
|
|
|
|
|
|
|
|
use Psalm\Internal\Codebase\ClassConstantByWildcardResolver;
|
|
|
|
use Psalm\Tests\TestCase;
|
|
|
|
use Psalm\Type\Atomic\TLiteralString;
|
|
|
|
|
2023-03-31 01:55:40 +02:00
|
|
|
use function reset;
|
|
|
|
|
2021-11-15 18:27:19 +01:00
|
|
|
final class ClassConstantByWildcardResolverTest extends TestCase
|
|
|
|
{
|
2022-12-16 19:58:47 +01:00
|
|
|
private ClassConstantByWildcardResolver $resolver;
|
2021-11-15 18:27:19 +01:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->resolver = new ClassConstantByWildcardResolver($this->project_analyzer->getCodebase());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWillParseAllClassConstants(): void
|
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'psalm-assert.php',
|
|
|
|
'
|
|
|
|
<?php
|
|
|
|
namespace ReconciliationTest;
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
const PREFIX_BAR = \'bar\';
|
|
|
|
const PREFIX_BAZ = \'baz\';
|
|
|
|
const PREFIX_QOO = Foo::PREFIX_BAR;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2021-11-15 18:27:19 +01:00
|
|
|
);
|
2023-03-31 01:55:40 +02:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$codebase->scanFiles();
|
2021-11-15 18:27:19 +01:00
|
|
|
$resolved = $this->resolver->resolve('ReconciliationTest\\Foo', '*');
|
|
|
|
self::assertNotEmpty($resolved);
|
|
|
|
foreach ($resolved as $type) {
|
|
|
|
self::assertInstanceOf(TLiteralString::class, $type);
|
|
|
|
self::assertTrue($type->value === 'bar' || $type->value === 'baz');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWillParseMatchingClassConstants(): void
|
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'psalm-assert.php',
|
|
|
|
'
|
|
|
|
<?php
|
|
|
|
namespace ReconciliationTest;
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
const BAR = \'bar\';
|
|
|
|
const BAZ = \'baz\';
|
|
|
|
const QOO = \'qoo\';
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2021-11-15 18:27:19 +01:00
|
|
|
);
|
2023-03-31 01:55:40 +02:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$codebase->scanFiles();
|
2021-11-15 18:27:19 +01:00
|
|
|
$resolved = $this->resolver->resolve('ReconciliationTest\\Foo', 'BA*');
|
|
|
|
self::assertNotEmpty($resolved);
|
|
|
|
foreach ($resolved as $type) {
|
|
|
|
self::assertInstanceOf(TLiteralString::class, $type);
|
|
|
|
self::assertTrue($type->value === 'bar' || $type->value === 'baz');
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolved = $this->resolver->resolve('ReconciliationTest\\Foo', 'QOO');
|
2021-11-15 18:31:28 +01:00
|
|
|
self::assertNotNull($resolved);
|
2021-11-15 18:27:19 +01:00
|
|
|
self::assertCount(1, $resolved);
|
2023-03-31 01:55:40 +02:00
|
|
|
$type = reset($resolved);
|
2021-11-15 18:27:19 +01:00
|
|
|
self::assertInstanceOf(TLiteralString::class, $type);
|
|
|
|
self::assertTrue($type->value === 'qoo');
|
|
|
|
}
|
|
|
|
}
|