1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

feature: ensure universal object crate class exists (#4375)

This commit is contained in:
feek 2020-10-20 14:17:49 -07:00 committed by Daniil Gentili
parent 7df404bfb5
commit d1b7cf6860
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 8 additions and 12 deletions

View File

@ -966,7 +966,7 @@ class Config
if (isset($config_xml->universalObjectCrates) && isset($config_xml->universalObjectCrates->class)) { if (isset($config_xml->universalObjectCrates) && isset($config_xml->universalObjectCrates->class)) {
/** @var \SimpleXMLElement $universal_object_crate */ /** @var \SimpleXMLElement $universal_object_crate */
foreach ($config_xml->universalObjectCrates->class as $universal_object_crate) { foreach ($config_xml->universalObjectCrates->class as $universal_object_crate) {
/** @var class-string $classString */ /** @var string $classString */
$classString = $universal_object_crate['name']; $classString = $universal_object_crate['name'];
$config->addUniversalObjectCrate($classString); $config->addUniversalObjectCrate($classString);
} }
@ -2007,11 +2007,11 @@ class Config
return null; return null;
} }
/**
* @param class-string $class
*/
public function addUniversalObjectCrate(string $class): void public function addUniversalObjectCrate(string $class): void
{ {
if (!class_exists($class, true)) {
throw new \UnexpectedValueException($class . ' is not a known class');
}
$this->universal_object_crates[] = $class; $this->universal_object_crates[] = $class;
} }

View File

@ -1300,12 +1300,12 @@ class ConfigTest extends \Psalm\Tests\TestCase
'<?xml version="1.0"?> '<?xml version="1.0"?>
<psalm> <psalm>
<universalObjectCrates> <universalObjectCrates>
<class name="Foo" /> <class name="DateTime" />
</universalObjectCrates> </universalObjectCrates>
</psalm>' </psalm>'
) )
); );
$this->assertContains('foo', $this->project_analyzer->getConfig()->getUniversalObjectCrates()); $this->assertContains('datetime', $this->project_analyzer->getConfig()->getUniversalObjectCrates());
} }
} }

View File

@ -120,16 +120,12 @@ class PropertyTypeTest extends TestCase
public function testUniversalObjectCrates(): void public function testUniversalObjectCrates(): void
{ {
/** @var class-string $classString */ Config::getInstance()->addUniversalObjectCrate(\DateTime::class);
$classString = 'Foo';
Config::getInstance()->addUniversalObjectCrate($classString);
$this->addFile( $this->addFile(
'somefile.php', 'somefile.php',
'<?php '<?php
class Foo { } $f = new \DateTime();
$f = new Foo();
// reads are fine // reads are fine
$f->bar; $f->bar;