2016-01-07 18:28:27 -05:00
|
|
|
<?php
|
2016-08-13 14:20:46 -04:00
|
|
|
namespace Psalm\Checker;
|
2016-01-07 18:28:27 -05:00
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
use PhpParser;
|
2017-07-25 16:11:02 -04:00
|
|
|
use Psalm\Aliases;
|
2016-12-03 19:11:30 -05:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 02:29:00 -04:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2018-01-01 21:17:23 -05:00
|
|
|
use Psalm\FileManipulation\FileManipulationBuffer;
|
2017-01-19 23:45:21 -05:00
|
|
|
use Psalm\Issue\DuplicateClass;
|
2017-01-01 19:09:17 -05:00
|
|
|
use Psalm\Issue\InaccessibleProperty;
|
2017-05-19 00:48:26 -04:00
|
|
|
use Psalm\Issue\InvalidClass;
|
2018-01-10 00:07:47 -05:00
|
|
|
use Psalm\Issue\ReservedWord;
|
2016-07-25 18:37:44 -04:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\IssueBuffer;
|
2017-08-22 12:38:38 -04:00
|
|
|
use Psalm\Provider\FileReferenceProvider;
|
2017-05-19 00:48:26 -04:00
|
|
|
use Psalm\StatementsSource;
|
2016-12-30 12:41:14 -05:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2016-11-02 02:29:00 -04:00
|
|
|
use Psalm\Type;
|
2016-01-07 18:28:27 -05:00
|
|
|
|
2016-11-20 21:49:06 -05:00
|
|
|
abstract class ClassLikeChecker extends SourceChecker implements StatementsSource
|
2016-01-07 18:28:27 -05:00
|
|
|
{
|
2017-01-01 19:09:17 -05:00
|
|
|
const VISIBILITY_PUBLIC = 1;
|
|
|
|
const VISIBILITY_PROTECTED = 2;
|
|
|
|
const VISIBILITY_PRIVATE = 3;
|
|
|
|
|
2016-10-31 15:42:20 -04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-01-02 15:31:18 -05:00
|
|
|
public static $SPECIAL_TYPES = [
|
|
|
|
'int' => 'int',
|
|
|
|
'string' => 'stirng',
|
|
|
|
'float' => 'float',
|
|
|
|
'bool' => 'bool',
|
|
|
|
'false' => 'false',
|
|
|
|
'object' => 'object',
|
|
|
|
'empty' => 'empty',
|
|
|
|
'callable' => 'callable',
|
|
|
|
'array' => 'array',
|
|
|
|
'iterable' => 'iterable',
|
|
|
|
'null' => 'null',
|
|
|
|
'mixed' => 'mixed',
|
2016-11-02 02:29:00 -04:00
|
|
|
];
|
2016-07-25 00:31:29 -04:00
|
|
|
|
2017-09-11 11:52:34 -04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $GETTYPE_TYPES = [
|
|
|
|
'boolean' => true,
|
|
|
|
'integer' => true,
|
|
|
|
'double' => true,
|
|
|
|
'string' => true,
|
|
|
|
'array' => true,
|
|
|
|
'object' => true,
|
|
|
|
'resource' => true,
|
|
|
|
'NULL' => true,
|
|
|
|
'unknown type' => true,
|
|
|
|
];
|
|
|
|
|
2016-09-09 09:13:41 -04:00
|
|
|
/**
|
|
|
|
* @var PhpParser\Node\Stmt\ClassLike
|
|
|
|
*/
|
2016-08-13 18:54:49 -04:00
|
|
|
protected $class;
|
2016-09-09 09:13:41 -04:00
|
|
|
|
2016-11-20 22:02:26 -05:00
|
|
|
/**
|
|
|
|
* @var StatementsSource
|
|
|
|
*/
|
|
|
|
protected $source;
|
|
|
|
|
2017-07-29 15:05:06 -04:00
|
|
|
/** @var FileChecker */
|
|
|
|
public $file_checker;
|
|
|
|
|
2016-09-09 09:13:41 -04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-11-07 17:29:51 -05:00
|
|
|
protected $fq_class_name;
|
2016-09-09 09:13:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parent class
|
|
|
|
*
|
2016-10-03 11:38:59 -04:00
|
|
|
* @var string|null
|
2016-09-09 09:13:41 -04:00
|
|
|
*/
|
2017-01-07 14:35:07 -05:00
|
|
|
protected $parent_fq_class_name;
|
2016-07-22 13:29:46 -04:00
|
|
|
|
2017-02-01 18:27:24 -05:00
|
|
|
/**
|
|
|
|
* @var PhpParser\Node\Stmt[]
|
|
|
|
*/
|
|
|
|
protected $leftover_stmts = [];
|
|
|
|
|
2017-07-29 15:05:06 -04:00
|
|
|
/** @var ClassLikeStorage */
|
|
|
|
protected $storage;
|
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\ClassLike $class
|
|
|
|
* @param StatementsSource $source
|
2016-11-07 17:29:51 -05:00
|
|
|
* @param string $fq_class_name
|
2016-11-02 02:29:00 -04:00
|
|
|
*/
|
2016-11-07 17:29:51 -05:00
|
|
|
public function __construct(PhpParser\Node\Stmt\ClassLike $class, StatementsSource $source, $fq_class_name)
|
2016-01-07 18:28:27 -05:00
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
$this->class = $class;
|
2016-11-12 18:51:48 -05:00
|
|
|
$this->source = $source;
|
2017-03-19 23:30:20 -04:00
|
|
|
$this->file_checker = $source->getFileChecker();
|
2016-11-07 17:29:51 -05:00
|
|
|
$this->fq_class_name = $fq_class_name;
|
2016-01-07 18:28:27 -05:00
|
|
|
|
2017-07-29 15:05:06 -04:00
|
|
|
$this->storage = $this->file_checker->project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-02-22 17:26:20 -05:00
|
|
|
|
2017-07-29 15:05:06 -04:00
|
|
|
if ($this->storage->location) {
|
|
|
|
$storage_file_path = $this->storage->location->file_path;
|
2017-07-25 16:11:02 -04:00
|
|
|
$source_file_path = $this->source->getCheckedFilePath();
|
2016-12-30 12:41:14 -05:00
|
|
|
|
2017-07-25 16:11:02 -04:00
|
|
|
if (!Config::getInstance()->use_case_sensitive_file_names) {
|
|
|
|
$storage_file_path = strtolower($storage_file_path);
|
|
|
|
$source_file_path = strtolower($source_file_path);
|
2017-02-12 13:25:59 -05:00
|
|
|
}
|
|
|
|
|
2017-07-25 16:11:02 -04:00
|
|
|
if ($storage_file_path !== $source_file_path ||
|
2017-07-29 15:05:06 -04:00
|
|
|
$this->storage->location->getLineNumber() !== $class->getLine()
|
2017-01-02 15:31:18 -05:00
|
|
|
) {
|
2017-02-09 20:35:17 -05:00
|
|
|
if (IssueBuffer::accepts(
|
2017-07-25 16:11:02 -04:00
|
|
|
new DuplicateClass(
|
|
|
|
'Class ' . $fq_class_name . ' has already been defined at ' .
|
2017-07-29 15:05:06 -04:00
|
|
|
$storage_file_path . ':' . $this->storage->location->getLineNumber(),
|
2018-01-01 21:17:23 -05:00
|
|
|
new CodeLocation($this, $class, null, true)
|
2017-02-09 20:35:17 -05:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-01-07 15:57:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 21:37:53 -05:00
|
|
|
/**
|
|
|
|
* @param string $method_name
|
|
|
|
* @param Context $context
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2017-01-11 21:37:53 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getMethodMutations(
|
|
|
|
$method_name,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-07-29 15:05:06 -04:00
|
|
|
$project_checker = $this->getFileChecker()->project_checker;
|
2018-01-21 13:38:51 -05:00
|
|
|
$codebase = $project_checker->codebase;
|
2017-07-29 15:05:06 -04:00
|
|
|
|
2017-01-11 21:37:53 -05:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
|
|
|
|
strtolower($stmt->name) === strtolower($method_name)
|
|
|
|
) {
|
2017-01-12 00:54:41 -05:00
|
|
|
$method_id = $this->fq_class_name . '::' . $stmt->name;
|
|
|
|
|
2018-02-03 18:52:35 -05:00
|
|
|
if ($project_checker->canCacheCheckers()) {
|
|
|
|
$method_checker = $codebase->methods->getCachedChecker($method_id);
|
2017-01-12 00:54:41 -05:00
|
|
|
|
2018-01-21 13:38:51 -05:00
|
|
|
if (!$method_checker) {
|
|
|
|
$method_checker = new MethodChecker($stmt, $this);
|
2018-02-03 18:52:35 -05:00
|
|
|
$codebase->methods->cacheChecker($method_id, $method_checker);
|
2017-01-12 00:54:41 -05:00
|
|
|
}
|
2018-01-21 13:38:51 -05:00
|
|
|
} else {
|
|
|
|
$method_checker = new MethodChecker($stmt, $this);
|
2017-01-12 00:54:41 -05:00
|
|
|
}
|
2017-01-11 21:37:53 -05:00
|
|
|
|
|
|
|
$method_checker->analyze($context, null, true);
|
2017-01-06 01:07:11 -05:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
|
|
|
foreach ($stmt->traits as $trait) {
|
2017-01-11 21:37:53 -05:00
|
|
|
$fq_trait_name = self::getFQCLNFromNameObject(
|
2017-01-06 01:07:11 -05:00
|
|
|
$trait,
|
2017-07-25 16:11:02 -04:00
|
|
|
$this->source->getAliases()
|
2017-01-06 01:07:11 -05:00
|
|
|
);
|
|
|
|
|
2018-02-03 18:52:35 -05:00
|
|
|
$trait_file_checker = $project_checker->getFileCheckerForClassLike($fq_trait_name);
|
|
|
|
$trait_node = $codebase->classlikes->getTraitNode($fq_trait_name);
|
|
|
|
$trait_aliases = $codebase->classlikes->getTraitAliases($fq_trait_name);
|
2018-01-21 12:44:46 -05:00
|
|
|
$trait_checker = new TraitChecker(
|
|
|
|
$trait_node,
|
|
|
|
$trait_file_checker,
|
|
|
|
$fq_trait_name,
|
|
|
|
$trait_aliases
|
|
|
|
);
|
2017-01-11 21:37:53 -05:00
|
|
|
|
2018-01-21 13:38:51 -05:00
|
|
|
foreach ($trait_node->stmts as $trait_stmt) {
|
2017-01-11 21:37:53 -05:00
|
|
|
if ($trait_stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
|
|
|
|
strtolower($trait_stmt->name) === strtolower($method_name)
|
|
|
|
) {
|
|
|
|
$method_checker = new MethodChecker($trait_stmt, $trait_checker);
|
|
|
|
|
|
|
|
$actual_method_id = (string)$method_checker->getMethodId();
|
2017-01-06 01:07:11 -05:00
|
|
|
|
2017-01-11 21:37:53 -05:00
|
|
|
if ($context->self && $context->self !== $this->fq_class_name) {
|
|
|
|
$analyzed_method_id = (string)$method_checker->getMethodId($context->self);
|
2018-02-03 18:52:35 -05:00
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($analyzed_method_id);
|
2017-01-11 21:37:53 -05:00
|
|
|
|
|
|
|
if ($actual_method_id !== $declaring_method_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_checker->analyze($context, null, true);
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 01:07:11 -05:00
|
|
|
}
|
2017-01-02 15:31:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 23:38:52 -04:00
|
|
|
/**
|
2016-11-07 17:29:51 -05:00
|
|
|
* @param string $fq_class_name
|
2016-11-04 20:10:59 -04:00
|
|
|
* @param array<string> $suppressed_issues
|
2017-03-02 12:19:18 -05:00
|
|
|
* @param bool $inferred - whether or not the type was inferred
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2016-07-24 23:38:52 -04:00
|
|
|
* @return bool|null
|
2016-04-26 18:42:48 -04:00
|
|
|
*/
|
2016-11-07 19:16:51 -05:00
|
|
|
public static function checkFullyQualifiedClassLikeName(
|
2018-01-01 20:04:03 -05:00
|
|
|
StatementsSource $statements_source,
|
2016-11-07 17:29:51 -05:00
|
|
|
$fq_class_name,
|
2016-12-03 19:11:30 -05:00
|
|
|
CodeLocation $code_location,
|
2017-03-02 12:19:18 -05:00
|
|
|
array $suppressed_issues,
|
|
|
|
$inferred = true
|
2016-11-02 02:29:00 -04:00
|
|
|
) {
|
2016-11-07 17:29:51 -05:00
|
|
|
if (empty($fq_class_name)) {
|
2016-04-12 11:59:27 -04:00
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2018-01-01 20:04:03 -05:00
|
|
|
$project_checker = $statements_source->getFileChecker()->project_checker;
|
2018-01-21 13:38:51 -05:00
|
|
|
$codebase = $project_checker->codebase;
|
2018-01-01 20:04:03 -05:00
|
|
|
|
2016-11-07 17:29:51 -05:00
|
|
|
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name);
|
2016-03-17 14:06:01 -04:00
|
|
|
|
2017-05-26 20:05:57 -04:00
|
|
|
if (in_array($fq_class_name, ['callable', 'iterable'], true)) {
|
2016-12-04 21:04:25 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-10 00:07:47 -05:00
|
|
|
if (preg_match(
|
|
|
|
'/(^|\\\)(int|float|bool|string|void|null|false|true|resource|object|numeric|mixed)$/i',
|
|
|
|
$fq_class_name
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$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
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-01 00:50:01 -05:00
|
|
|
$class_exists = $codebase->classExists($fq_class_name);
|
|
|
|
$interface_exists = $codebase->interfaceExists($fq_class_name);
|
2016-08-13 18:54:49 -04:00
|
|
|
|
|
|
|
if (!$class_exists && !$interface_exists) {
|
2016-06-26 15:18:40 -04:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 02:29:00 -04:00
|
|
|
new UndefinedClass(
|
2016-11-07 17:29:51 -05:00
|
|
|
'Class or interface ' . $fq_class_name . ' does not exist',
|
2016-12-03 19:11:30 -05:00
|
|
|
$code_location
|
2016-11-02 02:29:00 -04:00
|
|
|
),
|
2016-07-24 17:06:36 -04:00
|
|
|
$suppressed_issues
|
2016-06-05 20:25:16 -04:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 17:06:36 -04:00
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
return null;
|
2016-01-07 18:28:27 -05:00
|
|
|
}
|
|
|
|
|
2018-01-21 13:38:51 -05:00
|
|
|
if ($project_checker->getCodeBase()->collect_references && !$inferred) {
|
2017-07-29 15:05:06 -04:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-02-27 18:24:20 -05:00
|
|
|
if ($class_storage->referencing_locations === null) {
|
|
|
|
$class_storage->referencing_locations = [];
|
|
|
|
}
|
2017-07-29 15:05:06 -04:00
|
|
|
$class_storage->referencing_locations[$code_location->file_path][] = $code_location;
|
2017-02-26 23:09:18 -05:00
|
|
|
}
|
|
|
|
|
2018-01-21 13:38:51 -05:00
|
|
|
if (($class_exists && !$codebase->classHasCorrectCasing($fq_class_name)) ||
|
|
|
|
($interface_exists && !$codebase->interfaceHasCorrectCasing($fq_class_name))
|
2016-08-13 18:54:49 -04:00
|
|
|
) {
|
2018-02-03 18:52:35 -05:00
|
|
|
if ($codebase->classlikes->isUserDefined($fq_class_name)) {
|
2017-01-18 00:33:48 -05:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidClass(
|
|
|
|
'Class or interface ' . $fq_class_name . ' has wrong casing',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
2017-10-12 19:53:12 -04:00
|
|
|
// fall through here
|
2017-01-18 00:33:48 -05:00
|
|
|
}
|
2016-08-10 01:09:47 -04:00
|
|
|
}
|
2016-03-17 14:06:01 -04:00
|
|
|
}
|
|
|
|
|
2017-08-22 12:38:38 -04:00
|
|
|
FileReferenceProvider::addFileReferenceToClass(
|
|
|
|
$code_location->file_path,
|
|
|
|
strtolower($fq_class_name)
|
|
|
|
);
|
|
|
|
|
2018-01-01 21:17:23 -05:00
|
|
|
if (!$inferred) {
|
2018-02-11 20:56:34 -05:00
|
|
|
$plugin_method_ids = $codebase->config->after_classlike_exists_checks;
|
2018-01-01 21:17:23 -05:00
|
|
|
|
2018-02-11 20:56:34 -05:00
|
|
|
if ($plugin_method_ids) {
|
2018-01-01 21:17:23 -05:00
|
|
|
$file_manipulations = [];
|
|
|
|
|
2018-02-11 20:56:34 -05:00
|
|
|
foreach ($plugin_method_ids as $plugin_method_id) {
|
|
|
|
$plugin_method_id(
|
2018-01-01 21:17:23 -05:00
|
|
|
$statements_source,
|
|
|
|
$fq_class_name,
|
|
|
|
$code_location,
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
2018-02-11 20:56:34 -05:00
|
|
|
/** @psalm-suppress MixedTypeCoercion */
|
2018-01-01 21:17:23 -05:00
|
|
|
FileManipulationBuffer::add($code_location->file_path, $file_manipulations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 17:06:36 -04:00
|
|
|
return true;
|
2016-01-07 18:28:27 -05:00
|
|
|
}
|
|
|
|
|
2016-10-09 17:54:58 -04:00
|
|
|
/**
|
2016-11-02 02:29:00 -04:00
|
|
|
* Gets the fully-qualified class name from a Name object
|
2016-10-09 17:54:58 -04:00
|
|
|
*
|
2016-12-24 18:23:22 +00:00
|
|
|
* @param PhpParser\Node\Name $class_name
|
2018-01-18 12:10:57 -08:00
|
|
|
* @param Aliases $aliases
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2016-10-09 17:54:58 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-07-25 16:11:02 -04:00
|
|
|
public static function getFQCLNFromNameObject(PhpParser\Node\Name $class_name, Aliases $aliases)
|
|
|
|
{
|
2016-01-07 18:28:27 -05:00
|
|
|
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
|
2016-02-18 15:05:13 -05:00
|
|
|
return implode('\\', $class_name->parts);
|
2016-01-07 18:28:27 -05:00
|
|
|
}
|
|
|
|
|
2017-07-25 16:11:02 -04:00
|
|
|
if (in_array($class_name->parts[0], ['self', 'static', 'parent'], true)) {
|
|
|
|
return $class_name->parts[0];
|
|
|
|
}
|
|
|
|
|
2018-02-04 12:23:32 -05:00
|
|
|
return Type::getFQCLNFromString(
|
2017-01-06 01:07:11 -05:00
|
|
|
implode('\\', $class_name->parts),
|
2017-07-25 16:11:02 -04:00
|
|
|
$aliases
|
2017-01-06 01:07:11 -05:00
|
|
|
);
|
2016-01-07 18:28:27 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
/**
|
2018-01-28 17:07:09 -05:00
|
|
|
* @return null|string
|
2016-11-02 02:29:00 -04:00
|
|
|
*/
|
2016-01-19 18:27:06 -05:00
|
|
|
public function getNamespace()
|
|
|
|
{
|
2017-01-07 14:35:07 -05:00
|
|
|
return $this->source->getNamespace();
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:51:48 -05:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getAliasedClassesFlipped()
|
|
|
|
{
|
2016-11-13 11:24:46 -05:00
|
|
|
if ($this->source instanceof NamespaceChecker || $this->source instanceof FileChecker) {
|
|
|
|
return $this->source->getAliasedClassesFlipped();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
2016-11-12 18:51:48 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-07 19:16:51 -05:00
|
|
|
public function getFQCLN()
|
2016-01-19 18:27:06 -05:00
|
|
|
{
|
2016-11-07 17:29:51 -05:00
|
|
|
return $this->fq_class_name;
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
/**
|
2016-12-03 22:41:45 -05:00
|
|
|
* @return string|null
|
2016-11-02 02:29:00 -04:00
|
|
|
*/
|
2016-01-19 18:27:06 -05:00
|
|
|
public function getClassName()
|
|
|
|
{
|
2016-08-13 18:54:49 -04:00
|
|
|
return $this->class->name;
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-10-09 17:54:58 -04:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-01-07 14:35:07 -05:00
|
|
|
public function getParentFQCLN()
|
2016-01-19 18:27:06 -05:00
|
|
|
{
|
2017-01-07 14:35:07 -05:00
|
|
|
return $this->parent_fq_class_name;
|
2016-01-19 18:27:06 -05:00
|
|
|
}
|
|
|
|
|
2016-04-26 18:42:48 -04:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-19 18:27:06 -05:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-29 18:48:09 -05:00
|
|
|
|
2016-11-20 21:49:06 -05:00
|
|
|
/**
|
|
|
|
* Gets the Psalm type from a particular value
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2016-11-20 21:49:06 -05:00
|
|
|
* @return Type\Union
|
|
|
|
*/
|
|
|
|
public static function getTypeFromValue($value)
|
|
|
|
{
|
|
|
|
switch (gettype($value)) {
|
|
|
|
case 'boolean':
|
|
|
|
return Type::getBool();
|
|
|
|
|
|
|
|
case 'integer':
|
|
|
|
return Type::getInt();
|
|
|
|
|
|
|
|
case 'double':
|
|
|
|
return Type::getFloat();
|
|
|
|
|
|
|
|
case 'string':
|
|
|
|
return Type::getString();
|
|
|
|
|
|
|
|
case 'array':
|
|
|
|
return Type::getArray();
|
|
|
|
|
|
|
|
case 'NULL':
|
|
|
|
return Type::getNull();
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-01 19:09:17 -05:00
|
|
|
/**
|
|
|
|
* @param string $property_id
|
|
|
|
* @param string|null $calling_context
|
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param CodeLocation $code_location
|
|
|
|
* @param array $suppressed_issues
|
2018-01-13 01:52:46 -05:00
|
|
|
* @param bool $emit_issues
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2018-01-13 01:52:46 -05:00
|
|
|
* @return bool|null
|
2017-01-01 19:09:17 -05:00
|
|
|
*/
|
|
|
|
public static function checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$calling_context,
|
|
|
|
StatementsSource $source,
|
|
|
|
CodeLocation $code_location,
|
2018-01-13 01:52:46 -05:00
|
|
|
array $suppressed_issues,
|
|
|
|
$emit_issues = true
|
2017-01-01 19:09:17 -05:00
|
|
|
) {
|
2017-07-29 15:05:06 -04:00
|
|
|
$project_checker = $source->getFileChecker()->project_checker;
|
2018-02-01 00:50:01 -05:00
|
|
|
$codebase = $project_checker->codebase;
|
2017-07-29 15:05:06 -04:00
|
|
|
|
2018-02-08 18:14:28 -05:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty($property_id);
|
|
|
|
$appearing_property_class = $codebase->properties->getAppearingClassForProperty($property_id);
|
2017-01-01 19:09:17 -05:00
|
|
|
|
|
|
|
if (!$declaring_property_class || !$appearing_property_class) {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Appearing/Declaring classes are not defined for ' . $property_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:10:13 -05:00
|
|
|
list(, $property_name) = explode('::$', (string)$property_id);
|
2017-01-01 19:09:17 -05:00
|
|
|
|
|
|
|
// if the calling class is the same, we know the property exists, so it must be visible
|
|
|
|
if ($appearing_property_class === $calling_context) {
|
2018-01-13 01:52:46 -05:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-01 19:09:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($source->getSource() instanceof TraitChecker && $declaring_property_class === $source->getFQCLN()) {
|
2018-01-13 01:52:46 -05:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-01 19:09:17 -05:00
|
|
|
}
|
|
|
|
|
2017-07-29 15:05:06 -04:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($declaring_property_class);
|
2017-01-01 19:09:17 -05:00
|
|
|
|
2017-11-24 12:10:30 -05:00
|
|
|
if (!isset($class_storage->properties[$property_name])) {
|
2017-01-01 19:09:17 -05:00
|
|
|
throw new \UnexpectedValueException('$storage should not be null for ' . $property_id);
|
|
|
|
}
|
|
|
|
|
2017-11-24 12:10:30 -05:00
|
|
|
$storage = $class_storage->properties[$property_name];
|
|
|
|
|
2017-01-01 19:09:17 -05:00
|
|
|
switch ($storage->visibility) {
|
|
|
|
case self::VISIBILITY_PUBLIC:
|
2018-01-13 01:52:46 -05:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-01 19:09:17 -05:00
|
|
|
|
|
|
|
case self::VISIBILITY_PRIVATE:
|
|
|
|
if (!$calling_context || $appearing_property_class !== $calling_context) {
|
2018-01-13 02:08:53 -05:00
|
|
|
if ($emit_issues && IssueBuffer::accepts(
|
2017-01-01 19:09:17 -05:00
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access private property ' . $property_id . ' from context ' . $calling_context,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-13 01:52:46 -05:00
|
|
|
|
|
|
|
return null;
|
2017-01-01 19:09:17 -05:00
|
|
|
}
|
|
|
|
|
2018-01-13 01:52:46 -05:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-01 19:09:17 -05:00
|
|
|
|
|
|
|
case self::VISIBILITY_PROTECTED:
|
|
|
|
if ($appearing_property_class === $calling_context) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$calling_context) {
|
2018-01-13 02:08:53 -05:00
|
|
|
if ($emit_issues && IssueBuffer::accepts(
|
2017-01-01 19:09:17 -05:00
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access protected property ' . $property_id,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-01 00:50:01 -05:00
|
|
|
if ($codebase->classExtends($appearing_property_class, $calling_context)) {
|
2018-01-13 01:52:46 -05:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-01 19:09:17 -05:00
|
|
|
}
|
|
|
|
|
2018-02-01 00:50:01 -05:00
|
|
|
if (!$codebase->classExtends($calling_context, $appearing_property_class)) {
|
2018-01-13 02:08:53 -05:00
|
|
|
if ($emit_issues && IssueBuffer::accepts(
|
2017-01-01 19:09:17 -05:00
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access protected property ' . $property_id . ' from context ' . $calling_context,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-13 01:52:46 -05:00
|
|
|
|
|
|
|
return null;
|
2017-01-01 19:09:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 01:52:46 -05:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-01 19:09:17 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 02:29:00 -04:00
|
|
|
/**
|
2017-01-02 15:31:18 -05:00
|
|
|
* @param string $file_path
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2016-12-16 18:56:23 -05:00
|
|
|
* @return array<string>
|
2016-11-02 02:29:00 -04:00
|
|
|
*/
|
2017-07-29 15:05:06 -04:00
|
|
|
public static function getClassesForFile(ProjectChecker $project_checker, $file_path)
|
2016-10-04 19:23:38 -04:00
|
|
|
{
|
2017-07-29 15:05:06 -04:00
|
|
|
try {
|
|
|
|
return $project_checker->file_storage_provider->get($file_path)->classes_in_file;
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-10-04 19:23:38 -04:00
|
|
|
}
|
|
|
|
|
2017-11-26 16:03:17 -05:00
|
|
|
/**
|
|
|
|
* @return FileChecker
|
|
|
|
*/
|
2017-07-29 15:05:06 -04:00
|
|
|
public function getFileChecker()
|
|
|
|
{
|
|
|
|
return $this->file_checker;
|
|
|
|
}
|
2016-01-07 18:28:27 -05:00
|
|
|
}
|