1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Add config flag to support properties without a magic getter

This commit is contained in:
Matthew Brown 2020-02-02 14:23:30 -05:00
parent 6f6e26580a
commit 8d7fb2b415
3 changed files with 45 additions and 2 deletions

View File

@ -277,6 +277,11 @@ class Config
*/
public $use_phpdoc_method_without_magic_or_parent = false;
/**
* @var bool
*/
public $use_phpdoc_property_without_magic_or_parent = false;
/**
* @var bool
*/
@ -722,6 +727,7 @@ class Config
'allowPhpStormGenerics' => 'allow_phpstorm_generics',
'allowStringToStandInForClass' => 'allow_string_standin_for_class',
'usePhpDocMethodsWithoutMagicCall' => 'use_phpdoc_method_without_magic_or_parent',
'usePhpDocPropertiesWithoutMagicCall' => 'use_phpdoc_properties_without_magic_or_parent',
'memoizeMethodCallResults' => 'memoize_method_calls',
'hoistConstants' => 'hoist_constants',
'addParamDefaultToDocblockType' => 'add_param_default_to_docblock_type',

View File

@ -604,6 +604,9 @@ class PropertyFetchAnalyzer
);
}
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
$config = $statements_analyzer->getProjectAnalyzer()->getConfig();
if (!$codebase->properties->propertyExists(
$property_id,
true,
@ -612,6 +615,17 @@ class PropertyFetchAnalyzer
$context->collect_references ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null
)
) {
if ($config->use_phpdoc_property_without_magic_or_parent
&& isset($class_storage->pseudo_property_get_types['$' . $prop_name])
) {
$stmt_type = clone $class_storage->pseudo_property_get_types['$' . $prop_name];
$statements_analyzer->node_data->setType($stmt, $stmt_type);
self::processTaints($statements_analyzer, $stmt, $stmt_type, $property_id);
continue;
}
if ($fq_class_name !== $context->self
&& $context->self
&& $codebase->properties->propertyExists(
@ -797,8 +811,6 @@ class PropertyFetchAnalyzer
);
if ($lhs_type_part instanceof TGenericObject) {
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
if ($class_storage->template_types) {
$class_template_params = [];

View File

@ -1,6 +1,8 @@
<?php
namespace Psalm\Tests;
use Psalm\Config;
use Psalm\Context;
use const DIRECTORY_SEPARATOR;
class MagicPropertyTest extends TestCase
@ -8,6 +10,29 @@ class MagicPropertyTest extends TestCase
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
/**
* @return void
*/
public function testPhpDocPropertyWithoutGet()
{
Config::getInstance()->use_phpdoc_property_without_magic_or_parent = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @property string $hello
*/
class Child {}
$child = new Child();
$a = $child->hello;'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/