1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 02:47:02 +01:00
psalm/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php

648 lines
20 KiB
PHP
Raw Normal View History

2016-01-08 00:28:27 +01:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer;
2016-01-08 00:28:27 +01:00
2016-11-02 07:29:00 +01:00
use PhpParser;
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
use Psalm\Aliases;
use Psalm\CodeLocation;
2021-06-08 04:55:21 +02:00
use Psalm\Codebase;
2016-11-02 07:29:00 +01:00
use Psalm\Context;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Issue\InaccessibleProperty;
use Psalm\Issue\InvalidClass;
use Psalm\Issue\MissingDependency;
use Psalm\Issue\ReservedWord;
use Psalm\Issue\UndefinedAttributeClass;
2016-07-26 00:37:44 +02:00
use Psalm\Issue\UndefinedClass;
use Psalm\Issue\UndefinedDocblockClass;
2016-07-26 00:37:44 +02:00
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeExistenceCheckEvent;
use Psalm\StatementsSource;
use Psalm\Storage\ClassLikeStorage;
2016-11-02 07:29:00 +01:00
use Psalm\Type;
2021-06-08 04:55:21 +02:00
use function array_pop;
2021-06-08 04:55:21 +02:00
use function explode;
use function gettype;
2021-06-08 04:55:21 +02:00
use function implode;
use function in_array;
use function preg_match;
use function preg_replace;
use function strtolower;
2016-01-08 00:28:27 +01:00
/**
* @internal
*/
abstract class ClassLikeAnalyzer extends SourceAnalyzer
2016-01-08 00:28:27 +01:00
{
2020-09-20 18:54:46 +02:00
public const VISIBILITY_PUBLIC = 1;
public const VISIBILITY_PROTECTED = 2;
public const VISIBILITY_PRIVATE = 3;
2020-09-20 18:54:46 +02:00
public const SPECIAL_TYPES = [
'int' => 'int',
'string' => 'string',
'float' => 'float',
'bool' => 'bool',
'false' => 'false',
'object' => 'object',
'empty' => 'empty',
'callable' => 'callable',
'array' => 'array',
'iterable' => 'iterable',
'null' => 'null',
'mixed' => 'mixed',
2016-11-02 07:29:00 +01:00
];
2020-09-20 18:54:46 +02:00
public const GETTYPE_TYPES = [
2017-09-11 17:52:34 +02:00
'boolean' => true,
'integer' => true,
'double' => true,
'string' => true,
'array' => true,
'object' => true,
'resource' => true,
'resource (closed)' => true,
2017-09-11 17:52:34 +02:00
'NULL' => true,
'unknown type' => true,
];
2016-09-09 15:13:41 +02:00
/**
* @var PhpParser\Node\Stmt\ClassLike
*/
protected $class;
2016-09-09 15:13:41 +02:00
2018-11-06 03:57:36 +01:00
/** @var FileAnalyzer */
2018-11-11 18:01:14 +01:00
public $file_analyzer;
2016-09-09 15:13:41 +02:00
/**
* @var string
*/
protected $fq_class_name;
2016-09-09 15:13:41 +02:00
/**
* The parent class
*
2016-10-03 17:38:59 +02:00
* @var string|null
2016-09-09 15:13:41 +02:00
*/
2017-01-07 20:35:07 +01:00
protected $parent_fq_class_name;
/**
* @var PhpParser\Node\Stmt[]
*/
protected $leftover_stmts = [];
/** @var ClassLikeStorage */
protected $storage;
public function __construct(PhpParser\Node\Stmt\ClassLike $class, SourceAnalyzer $source, string $fq_class_name)
2016-01-08 00:28:27 +01:00
{
$this->class = $class;
2016-11-13 00:51:48 +01:00
$this->source = $source;
2018-11-11 18:01:14 +01:00
$this->file_analyzer = $source->getFileAnalyzer();
$this->fq_class_name = $fq_class_name;
$codebase = $source->getCodebase();
$this->storage = $codebase->classlike_storage_provider->get($fq_class_name);
2017-01-07 21:57:25 +01:00
}
public function __destruct()
{
2021-09-25 18:11:54 +02:00
unset($this->source);
unset($this->file_analyzer);
}
2017-01-12 03:37:53 +01:00
public function getMethodMutations(
string $method_name,
2017-01-12 03:37:53 +01:00
Context $context
): void {
2018-11-11 18:01:14 +01:00
$project_analyzer = $this->getFileAnalyzer()->project_analyzer;
$codebase = $project_analyzer->getCodebase();
2017-01-12 03:37:53 +01:00
foreach ($this->class->stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
strtolower($stmt->name->name) === strtolower($method_name)
2017-01-12 03:37:53 +01:00
) {
2018-11-11 18:01:14 +01:00
$method_analyzer = new MethodAnalyzer($stmt, $this);
2017-01-12 03:37:53 +01:00
$method_analyzer->analyze($context, new \Psalm\Internal\Provider\NodeDataProvider(), null, true);
$context->clauses = [];
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
foreach ($stmt->traits as $trait) {
2017-01-12 03:37:53 +01:00
$fq_trait_name = self::getFQCLNFromNameObject(
$trait,
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
$this->source->getAliases()
);
2018-11-11 18:01:14 +01:00
$trait_file_analyzer = $project_analyzer->getFileAnalyzerForClassLike($fq_trait_name);
2018-02-04 00:52:35 +01:00
$trait_node = $codebase->classlikes->getTraitNode($fq_trait_name);
2020-03-03 04:27:54 +01:00
$trait_storage = $codebase->classlike_storage_provider->get($fq_trait_name);
$trait_aliases = $trait_storage->aliases;
if ($trait_aliases === null) {
continue;
}
2018-11-11 18:01:14 +01:00
$trait_analyzer = new TraitAnalyzer(
2018-01-21 18:44:46 +01:00
$trait_node,
2018-11-11 18:01:14 +01:00
$trait_file_analyzer,
2018-01-21 18:44:46 +01:00
$fq_trait_name,
$trait_aliases
);
2017-01-12 03:37:53 +01:00
foreach ($trait_node->stmts as $trait_stmt) {
2017-01-12 03:37:53 +01:00
if ($trait_stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
strtolower($trait_stmt->name->name) === strtolower($method_name)
2017-01-12 03:37:53 +01:00
) {
2018-11-11 18:01:14 +01:00
$method_analyzer = new MethodAnalyzer($trait_stmt, $trait_analyzer);
2017-01-12 03:37:53 +01:00
$actual_method_id = $method_analyzer->getMethodId();
2017-01-12 03:37:53 +01:00
if ($context->self && $context->self !== $this->fq_class_name) {
$analyzed_method_id = $method_analyzer->getMethodId($context->self);
2018-02-04 00:52:35 +01:00
$declaring_method_id = $codebase->methods->getDeclaringMethodId($analyzed_method_id);
2017-01-12 03:37:53 +01:00
if ((string) $actual_method_id !== (string) $declaring_method_id) {
2017-01-12 03:37:53 +01:00
break;
}
}
$method_analyzer->analyze(
$context,
new \Psalm\Internal\Provider\NodeDataProvider(),
null,
true
);
2017-01-12 03:37:53 +01:00
}
}
2019-06-30 03:06:21 +02:00
$trait_file_analyzer->clearSourceBeforeDestruction();
}
}
}
}
2020-10-17 18:36:44 +02:00
public function getFunctionLikeAnalyzer(string $method_name) : ?MethodAnalyzer
{
foreach ($this->class->stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
strtolower($stmt->name->name) === strtolower($method_name)
) {
return new MethodAnalyzer($stmt, $this);
}
}
return null;
}
2016-07-25 05:38:52 +02:00
/**
2016-11-05 01:10:59 +01:00
* @param array<string> $suppressed_issues
2016-04-27 00:42:48 +02:00
*/
2016-11-08 01:16:51 +01:00
public static function checkFullyQualifiedClassLikeName(
StatementsSource $statements_source,
string $fq_class_name,
CodeLocation $code_location,
?string $calling_fq_class_name,
?string $calling_method_id,
array $suppressed_issues,
?ClassLikeNameOptions $options = null
): ?bool {
if ($options === null) {
$options = new ClassLikeNameOptions();
}
2018-11-06 03:57:36 +01:00
$codebase = $statements_source->getCodebase();
if ($fq_class_name === '') {
if (IssueBuffer::accepts(
new UndefinedClass(
'Class or interface <empty string> does not exist',
$code_location,
'empty string'
),
$suppressed_issues
)) {
return false;
}
return null;
2016-04-12 17:59:27 +02:00
}
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name);
2016-03-17 19:06:01 +01:00
if (in_array($fq_class_name, ['callable', 'iterable', 'self', 'static', 'parent'], true)) {
return true;
}
if (preg_match(
'/(^|\\\)(int|float|bool|string|void|null|false|true|object|mixed)$/i',
$fq_class_name
) || strtolower($fq_class_name) === 'resource'
) {
$class_name_parts = explode('\\', $fq_class_name);
$class_name = array_pop($class_name_parts);
if (IssueBuffer::accepts(
new ReservedWord(
$class_name . ' is a reserved word',
$code_location,
$class_name
),
$suppressed_issues
)) {
// fall through
}
return null;
}
$class_exists = $codebase->classlikes->classExists(
$fq_class_name,
!$options->inferred ? $code_location : null,
$calling_fq_class_name,
$calling_method_id
);
$interface_exists = $codebase->classlikes->interfaceExists(
$fq_class_name,
!$options->inferred ? $code_location : null,
$calling_fq_class_name,
$calling_method_id
);
$enum_exists = $codebase->classlikes->enumExists(
$fq_class_name,
!$options->inferred ? $code_location : null,
$calling_fq_class_name,
$calling_method_id
);
if (!$class_exists
&& !($interface_exists && $options->allow_interface)
&& !($enum_exists && $options->allow_enum)
) {
if (!$options->allow_trait || !$codebase->classlikes->traitExists($fq_class_name, $code_location)) {
if ($options->from_docblock) {
if (IssueBuffer::accepts(
new UndefinedDocblockClass(
'Docblock-defined class, interface or enum named ' . $fq_class_name . ' does not exist',
$code_location,
$fq_class_name
),
$suppressed_issues
)) {
return false;
}
} elseif ($options->from_attribute) {
if (IssueBuffer::accepts(
new UndefinedAttributeClass(
'Attribute class ' . $fq_class_name . ' does not exist',
$code_location,
$fq_class_name
),
$suppressed_issues
)) {
return false;
}
} else {
if (IssueBuffer::accepts(
new UndefinedClass(
'Class, interface or enum named ' . $fq_class_name . ' does not exist',
$code_location,
$fq_class_name
),
$suppressed_issues
)) {
return false;
}
2018-05-09 04:32:57 +02:00
}
}
2016-11-02 07:29:00 +01:00
return null;
2016-01-08 00:28:27 +01:00
}
$aliased_name = $codebase->classlikes->getUnAliasedName(
$fq_class_name
2018-11-29 06:05:56 +01:00
);
try {
$class_storage = $codebase->classlike_storage_provider->get($aliased_name);
} catch (\InvalidArgumentException $e) {
if (!$options->inferred) {
throw $e;
}
return null;
}
foreach ($class_storage->invalid_dependencies as $dependency_class_name) {
// if the implemented/extended class is stubbed, it may not yet have
// been hydrated
if ($codebase->classlike_storage_provider->has($dependency_class_name)) {
continue;
}
if (IssueBuffer::accepts(
new MissingDependency(
$fq_class_name . ' depends on class or interface '
. $dependency_class_name . ' that does not exist',
$code_location,
$fq_class_name
),
$suppressed_issues
)) {
return false;
}
}
if (!$options->inferred) {
if (($class_exists && !$codebase->classHasCorrectCasing($fq_class_name))
|| ($interface_exists && !$codebase->interfaceHasCorrectCasing($fq_class_name))
|| ($enum_exists && !$codebase->classlikes->enumHasCorrectCasing($fq_class_name))
) {
if ($codebase->classlikes->isUserDefined(strtolower($aliased_name))) {
if (IssueBuffer::accepts(
new InvalidClass(
'Class, interface or enum ' . $fq_class_name . ' has wrong casing',
$code_location,
$fq_class_name
),
$suppressed_issues
)) {
// fall through here
}
}
2016-08-10 07:09:47 +02:00
}
2016-03-17 19:06:01 +01:00
}
if (!$options->inferred) {
$event = new AfterClassLikeExistenceCheckEvent(
$fq_class_name,
$code_location,
$statements_source,
$codebase,
[]
);
$codebase->config->eventDispatcher->dispatchAfterClassLikeExistenceCheck($event);
$file_manipulations = $event->getFileReplacements();
if ($file_manipulations) {
FileManipulationBuffer::add($code_location->file_path, $file_manipulations);
}
}
return true;
2016-01-08 00:28:27 +01:00
}
/**
2016-11-02 07:29:00 +01:00
* Gets the fully-qualified class name from a Name object
*
2017-05-27 02:16:18 +02:00
*
*/
public static function getFQCLNFromNameObject(
PhpParser\Node\Name $class_name,
Aliases $aliases
): string {
/** @var string|null */
$resolved_name = $class_name->getAttribute('resolvedName');
if ($resolved_name) {
return $resolved_name;
}
2016-01-08 00:28:27 +01:00
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
return implode('\\', $class_name->parts);
2016-01-08 00:28:27 +01:00
}
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
if (in_array($class_name->parts[0], ['self', 'static', 'parent'], true)) {
return $class_name->parts[0];
}
return Type::getFQCLNFromString(
implode('\\', $class_name->parts),
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
$aliases
);
2016-01-08 00:28:27 +01:00
}
2016-11-13 00:51:48 +01:00
/**
* @return array<lowercase-string, string>
2016-11-13 00:51:48 +01:00
*/
public function getAliasedClassesFlipped(): array
2016-11-13 00:51:48 +01:00
{
2018-11-06 03:57:36 +01:00
if ($this->source instanceof NamespaceAnalyzer || $this->source instanceof FileAnalyzer) {
2016-11-13 17:24:46 +01:00
return $this->source->getAliasedClassesFlipped();
}
return [];
2016-11-13 00:51:48 +01:00
}
2019-06-06 04:13:33 +02:00
/**
* @return array<string, string>
*/
public function getAliasedClassesFlippedReplaceable(): array
2019-06-06 04:13:33 +02:00
{
if ($this->source instanceof NamespaceAnalyzer || $this->source instanceof FileAnalyzer) {
return $this->source->getAliasedClassesFlippedReplaceable();
}
return [];
}
public function getFQCLN(): string
{
return $this->fq_class_name;
}
public function getClassName(): ?string
{
2021-09-26 22:57:04 +02:00
return $this->class->name->name ?? null;
}
/**
* @return array<string, array<string, Type\Union>>|null
*/
public function getTemplateTypeMap(): ?array
{
return $this->storage->template_types;
}
public function getParentFQCLN(): ?string
{
2017-01-07 20:35:07 +01:00
return $this->parent_fq_class_name;
}
public function isStatic(): bool
{
return false;
}
2016-11-21 03:49:06 +01:00
/**
* Gets the Psalm type from a particular value
*
* @param mixed $value
2017-05-27 02:16:18 +02:00
*
2016-11-21 03:49:06 +01:00
*/
public static function getTypeFromValue($value): Type\Union
2016-11-21 03:49:06 +01:00
{
switch (gettype($value)) {
case 'boolean':
if ($value) {
return Type::getTrue();
}
return Type::getFalse();
2016-11-21 03:49:06 +01:00
case 'integer':
return Type::getInt(false, $value);
2016-11-21 03:49:06 +01:00
case 'double':
return Type::getFloat($value);
2016-11-21 03:49:06 +01:00
case 'string':
return Type::getString($value);
2016-11-21 03:49:06 +01:00
case 'array':
return Type::getArray();
case 'NULL':
return Type::getNull();
default:
return Type::getMixed();
}
}
/**
* @param string[] $suppressed_issues
*/
public static function checkPropertyVisibility(
string $property_id,
Context $context,
2018-11-06 03:57:36 +01:00
SourceAnalyzer $source,
CodeLocation $code_location,
array $suppressed_issues,
bool $emit_issues = true
): ?bool {
[$fq_class_name, $property_name] = explode('::$', $property_id);
2018-11-06 03:57:36 +01:00
$codebase = $source->getCodebase();
if ($codebase->properties->property_visibility_provider->has($fq_class_name)) {
$property_visible = $codebase->properties->property_visibility_provider->isPropertyVisible(
$source,
$fq_class_name,
$property_name,
2020-01-06 00:53:24 +01:00
true,
2020-08-31 22:40:16 +02:00
$context,
$code_location
);
if ($property_visible !== null) {
return $property_visible;
}
}
2018-11-29 06:05:56 +01:00
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
$property_id,
true
2018-11-29 06:05:56 +01:00
);
$appearing_property_class = $codebase->properties->getAppearingClassForProperty(
$property_id,
true
2018-11-29 06:05:56 +01:00
);
if (!$declaring_property_class || !$appearing_property_class) {
throw new \UnexpectedValueException(
'Appearing/Declaring classes are not defined for ' . $property_id
);
}
// if the calling class is the same, we know the property exists, so it must be visible
if ($appearing_property_class === $context->self) {
return $emit_issues ? null : true;
}
if ($source->getSource() instanceof TraitAnalyzer
&& strtolower($declaring_property_class) === strtolower((string) $source->getFQCLN())
) {
return $emit_issues ? null : true;
}
$class_storage = $codebase->classlike_storage_provider->get($declaring_property_class);
if (!isset($class_storage->properties[$property_name])) {
throw new \UnexpectedValueException('$storage should not be null for ' . $property_id);
}
$storage = $class_storage->properties[$property_name];
switch ($storage->visibility) {
case self::VISIBILITY_PUBLIC:
return $emit_issues ? null : true;
case self::VISIBILITY_PRIVATE:
2021-09-26 22:19:42 +02:00
if ($emit_issues && IssueBuffer::accepts(
new InaccessibleProperty(
'Cannot access private property ' . $property_id . ' from context ' . $context->self,
$code_location
),
$suppressed_issues
)) {
// fall through
}
2021-09-26 22:19:42 +02:00
return null;
case self::VISIBILITY_PROTECTED:
if (!$context->self) {
2018-01-13 08:08:53 +01:00
if ($emit_issues && IssueBuffer::accepts(
new InaccessibleProperty(
'Cannot access protected property ' . $property_id,
$code_location
),
$suppressed_issues
)) {
2020-03-15 04:54:42 +01:00
// fall through
}
return null;
}
if ($codebase->classExtends($appearing_property_class, $context->self)) {
return $emit_issues ? null : true;
}
if (!$codebase->classExtends($context->self, $appearing_property_class)) {
2018-01-13 08:08:53 +01:00
if ($emit_issues && IssueBuffer::accepts(
new InaccessibleProperty(
'Cannot access protected property ' . $property_id . ' from context ' . $context->self,
$code_location
),
$suppressed_issues
)) {
2020-03-15 04:54:42 +01:00
// fall through
}
return null;
}
}
return $emit_issues ? null : true;
}
2016-11-02 07:29:00 +01:00
/**
* @return array<string, string>
2016-11-02 07:29:00 +01:00
*/
public static function getClassesForFile(Codebase $codebase, string $file_path): array
{
try {
2018-10-07 02:11:19 +02:00
return $codebase->file_storage_provider->get($file_path)->classlikes_in_file;
} catch (\InvalidArgumentException $e) {
return [];
}
}
2018-11-06 03:57:36 +01:00
public function getFileAnalyzer() : FileAnalyzer
{
2018-11-11 18:01:14 +01:00
return $this->file_analyzer;
}
2016-01-08 00:28:27 +01:00
}