2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
use PhpParser;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2016-11-21 05:31:10 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2017-01-20 05:45:21 +01:00
|
|
|
use Psalm\Issue\DuplicateClass;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\InvalidClass;
|
2016-11-21 05:31:10 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2017-01-02 01:09:17 +01:00
|
|
|
use Psalm\Issue\InaccessibleMethod;
|
|
|
|
use Psalm\Issue\InaccessibleProperty;
|
2017-01-27 07:23:12 +01:00
|
|
|
use Psalm\Issue\MissingConstructor;
|
2016-10-31 20:17:54 +01:00
|
|
|
use Psalm\Issue\MissingPropertyType;
|
2017-01-27 07:23:12 +01:00
|
|
|
use Psalm\Issue\PropertyNotSetInConstructor;
|
2017-01-07 20:35:07 +01:00
|
|
|
use Psalm\Issue\RedefinedTraitMethod;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\Issue\UndefinedTrait;
|
2016-10-30 16:14:36 +01:00
|
|
|
use Psalm\Issue\UnimplementedInterfaceMethod;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\IssueBuffer;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\StatementsSource;
|
2016-12-30 18:41:14 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2017-01-02 01:09:17 +01:00
|
|
|
use Psalm\Storage\MethodStorage;
|
|
|
|
use Psalm\Storage\PropertyStorage;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Type;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2016-04-17 17:22:18 +02:00
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionMethod;
|
2016-08-05 21:11:20 +02:00
|
|
|
use ReflectionProperty;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
abstract class ClassLikeChecker extends SourceChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2017-01-02 01:09:17 +01:00
|
|
|
const VISIBILITY_PUBLIC = 1;
|
|
|
|
const VISIBILITY_PROTECTED = 2;
|
|
|
|
const VISIBILITY_PRIVATE = 3;
|
|
|
|
|
2016-10-31 20:42:20 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-01-02 21:31:18 +01: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 07:29:00 +01:00
|
|
|
];
|
2016-07-25 06:31:29 +02:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* @var PhpParser\Node\Stmt\ClassLike
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $class;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
2016-11-21 04:02:26 +01:00
|
|
|
/**
|
|
|
|
* @var StatementsSource
|
|
|
|
*/
|
|
|
|
protected $source;
|
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
protected $fq_class_name;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $has_custom_get = false;
|
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;
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-07-25 00:02:03 +02:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @var array<string, MethodChecker>
|
2016-07-25 00:02:03 +02:00
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
protected $method_checkers = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, MethodChecker>
|
|
|
|
*/
|
|
|
|
protected $property_types = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
|
2016-12-12 20:29:58 +01:00
|
|
|
/**
|
2016-12-30 18:41:14 +01:00
|
|
|
* @var array<string, array<string, string>>|null
|
2016-12-12 20:29:58 +01:00
|
|
|
*/
|
2016-12-30 18:41:14 +01:00
|
|
|
protected static $property_map;
|
2016-12-12 20:29:58 +01:00
|
|
|
|
|
|
|
/**
|
2016-12-30 18:41:14 +01:00
|
|
|
* @var array<string, ClassLikeStorage>
|
2016-12-12 20:29:58 +01:00
|
|
|
*/
|
2016-12-30 21:53:35 +01:00
|
|
|
public static $storage = [];
|
2016-12-12 20:29:58 +01:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* A lookup table of cached TraitCheckers
|
2016-09-09 15:13:41 +02:00
|
|
|
*
|
2017-01-02 21:31:18 +01:00
|
|
|
* @var array<string, TraitChecker>
|
|
|
|
*/
|
|
|
|
public static $trait_checkers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table of cached ClassCheckers
|
|
|
|
*
|
|
|
|
* @var array<string, ClassChecker>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-12-30 18:41:14 +01:00
|
|
|
public static $class_checkers;
|
2016-10-05 01:23:38 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-30 18:41:14 +01:00
|
|
|
* @var array<string, array<string, string>>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-12-30 18:41:14 +01:00
|
|
|
public static $file_classes = [];
|
2016-12-12 19:50:46 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
/**
|
|
|
|
* A lookup table used for caching the results of classExtends calls
|
|
|
|
*
|
|
|
|
* @var array<string, array<string, bool>>
|
|
|
|
*/
|
|
|
|
protected static $class_extends = [];
|
|
|
|
|
2017-02-02 00:27:24 +01:00
|
|
|
/**
|
|
|
|
* @var PhpParser\Node\Stmt[]
|
|
|
|
*/
|
|
|
|
protected $leftover_stmts = [];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\ClassLike $class
|
|
|
|
* @param StatementsSource $source
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
public function __construct(PhpParser\Node\Stmt\ClassLike $class, StatementsSource $source, $fq_class_name)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->class = $class;
|
2016-11-13 00:51:48 +01:00
|
|
|
$this->source = $source;
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->fq_class_name = $fq_class_name;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$fq_class_name_lower = strtolower($fq_class_name);
|
|
|
|
|
|
|
|
if (!isset(self::$storage[$fq_class_name_lower])) {
|
|
|
|
self::$storage[$fq_class_name_lower] = $storage = new ClassLikeStorage();
|
2017-02-08 06:28:26 +01:00
|
|
|
$storage->name = $fq_class_name;
|
|
|
|
$storage->location = new CodeLocation($this->source, $class, true);
|
2017-01-18 06:33:48 +01:00
|
|
|
$storage->user_defined = true;
|
2017-01-20 05:45:21 +01:00
|
|
|
} else {
|
|
|
|
$storage = self::$storage[$fq_class_name_lower];
|
|
|
|
|
2017-02-08 06:28:26 +01:00
|
|
|
if ($storage->location &&
|
|
|
|
($storage->location->file_path !== $this->source->getFilePath() ||
|
|
|
|
$storage->location->getLineNumber() !== $class->getLine()
|
|
|
|
)
|
|
|
|
) {
|
2017-01-20 05:45:21 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateClass(
|
|
|
|
'Class ' . $fq_class_name . ' has already been defined at ' .
|
2017-02-08 06:28:26 +01:00
|
|
|
$storage->location->file_path . ':' . $storage->location->getLineNumber(),
|
2017-01-20 05:45:21 +01:00
|
|
|
new \Psalm\CodeLocation($this, $class, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2016-12-30 18:41:14 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
self::$file_classes[$this->source->getFilePath()][] = $fq_class_name;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-11-13 17:24:46 +01:00
|
|
|
* @param Context|null $class_context
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param Context|null $global_context
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public function visit(
|
2017-01-02 21:31:18 +01:00
|
|
|
Context $class_context = null,
|
|
|
|
Context $global_context = null
|
|
|
|
) {
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[strtolower($class_context ? (string)$class_context->self : $this->fq_class_name)];
|
2016-12-30 18:41:14 +01:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!($this instanceof TraitChecker) && $storage->registered) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->user_defined = true;
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
$long_file_name = $this->source->getFilePath();
|
2016-10-25 00:49:07 +02:00
|
|
|
|
2016-08-15 05:24:16 +02:00
|
|
|
if (!$class_context) {
|
2017-01-17 00:33:04 +01:00
|
|
|
$class_context = new Context($this->fq_class_name);
|
2017-01-07 20:35:07 +01:00
|
|
|
$class_context->parent = $this->parent_fq_class_name;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
|
|
|
if ($global_context) {
|
|
|
|
$class_context->vars_in_scope = $global_context->vars_in_scope;
|
|
|
|
$class_context->vars_possibly_in_scope = $global_context->vars_possibly_in_scope;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
$class_context->vars_in_scope['$this'] = new Type\Union([new TNamedObject($this->fq_class_name)]);
|
2017-02-08 08:23:17 +01:00
|
|
|
$class_context->vars_possibly_in_scope['$this'] = true;
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
// set all constants first
|
2016-08-15 05:24:16 +02:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
|
|
|
foreach ($stmt->consts as $const) {
|
2016-12-04 07:44:33 +01:00
|
|
|
if ($stmt->isProtected()) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->protected_class_constants[$const->name] = Type::getMixed();
|
2016-12-04 07:44:33 +01:00
|
|
|
} elseif ($stmt->isPrivate()) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->private_class_constants[$const->name] = Type::getMixed();
|
2016-12-04 07:44:33 +01:00
|
|
|
} else {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants[$const->name] = Type::getMixed();
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 18:06:53 +02:00
|
|
|
|
2016-08-14 01:44:24 +02:00
|
|
|
if ($this instanceof ClassChecker) {
|
2017-01-07 20:35:07 +01:00
|
|
|
if ($this->parent_fq_class_name &&
|
|
|
|
$this->registerParentClassInfo($this->parent_fq_class_name) === false
|
2017-01-02 21:31:18 +01:00
|
|
|
) {
|
2016-11-02 14:24:36 +01:00
|
|
|
return false;
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2016-11-20 17:51:19 +01:00
|
|
|
if ($this instanceof InterfaceChecker || $this instanceof ClassChecker) {
|
2016-10-25 00:49:07 +02:00
|
|
|
$extra_interfaces = [];
|
|
|
|
|
2016-11-20 17:51:19 +01:00
|
|
|
if ($this instanceof InterfaceChecker) {
|
2017-01-02 21:31:18 +01:00
|
|
|
$parent_interfaces = InterfaceChecker::getParentInterfaces(
|
|
|
|
$this->fq_class_name
|
|
|
|
);
|
|
|
|
|
2016-11-20 17:51:19 +01:00
|
|
|
$extra_interfaces = $parent_interfaces;
|
2016-12-29 03:37:24 +01:00
|
|
|
} else {
|
2017-01-09 05:58:06 +01:00
|
|
|
$parent_interfaces = self::$storage[strtolower($this->fq_class_name)]->class_implements;
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($parent_interfaces as $interface_name) {
|
2016-11-08 01:16:51 +01:00
|
|
|
if (self::checkFullyQualifiedClassLikeName(
|
2016-08-15 05:24:16 +02:00
|
|
|
$interface_name,
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->getFileChecker(),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->class, true),
|
2017-01-16 06:13:08 +01:00
|
|
|
$this->getSuppressedIssues(),
|
|
|
|
true
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-08-15 05:24:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$extra_interfaces = array_merge(
|
|
|
|
$extra_interfaces,
|
2017-01-02 21:31:18 +01:00
|
|
|
InterfaceChecker::getParentInterfaces(
|
|
|
|
$interface_name
|
|
|
|
)
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-10-25 00:49:07 +02:00
|
|
|
|
2017-01-16 06:13:08 +01:00
|
|
|
$interface_storage = self::$storage[strtolower($interface_name)];
|
|
|
|
|
|
|
|
// copy over any constants
|
|
|
|
$storage->public_class_constants = array_merge(
|
|
|
|
$storage->public_class_constants,
|
|
|
|
$interface_storage->public_class_constants
|
|
|
|
);
|
|
|
|
|
2016-10-25 00:49:07 +02:00
|
|
|
FileChecker::addFileInheritanceToClass($long_file_name, $interface_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$extra_interfaces = array_unique($extra_interfaces);
|
2016-10-05 23:08:20 +02:00
|
|
|
|
2016-10-25 00:49:07 +02:00
|
|
|
foreach ($extra_interfaces as $extra_interface_name) {
|
|
|
|
FileChecker::addFileInheritanceToClass($long_file_name, $extra_interface_name);
|
|
|
|
|
2016-11-20 17:51:19 +01:00
|
|
|
if ($this instanceof ClassChecker) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->class_implements[strtolower($extra_interface_name)] = $extra_interface_name;
|
2016-12-29 03:37:24 +01:00
|
|
|
} else {
|
2017-01-02 01:50:29 +01:00
|
|
|
$this->registerInheritedMethods($this->fq_class_name, $extra_interface_name);
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
2016-11-02 17:14:21 +01:00
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2016-08-05 21:11:20 +02:00
|
|
|
|
2017-02-02 00:27:24 +01:00
|
|
|
$const_stmts = [];
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->visitClassMethod(
|
|
|
|
$stmt,
|
2017-01-02 21:31:18 +01:00
|
|
|
$class_context
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-11-02 14:24:36 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->visitTraitUse(
|
|
|
|
$stmt,
|
|
|
|
$class_context
|
|
|
|
);
|
2016-11-02 14:24:36 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->visitPropertyDeclaration(
|
|
|
|
$stmt,
|
|
|
|
$class_context,
|
2017-01-02 21:31:18 +01:00
|
|
|
$config
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2017-02-02 00:27:24 +01:00
|
|
|
$this->leftover_stmts[] = $stmt;
|
2016-11-02 14:24:36 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->visitClassConstDeclaration(
|
|
|
|
$stmt,
|
|
|
|
$class_context,
|
|
|
|
$config
|
|
|
|
);
|
2017-02-02 00:27:24 +01:00
|
|
|
$const_stmts[] = $stmt;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-26 20:13:04 +01:00
|
|
|
|
2017-01-30 05:44:05 +01:00
|
|
|
if (MethodChecker::methodExists($this->fq_class_name . '::__get', $this->getFileChecker())) {
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->has_custom_get = true;
|
2016-04-20 12:55:26 +02:00
|
|
|
}
|
|
|
|
|
2017-02-02 00:27:24 +01:00
|
|
|
if ($const_stmts) {
|
|
|
|
(new StatementsChecker($this))->analyze($const_stmts, $class_context);
|
2016-01-26 20:13:04 +01:00
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
|
2016-06-21 00:09:04 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-12-28 22:46:30 +01:00
|
|
|
if ($this instanceof ClassChecker && $this->class instanceof PhpParser\Node\Stmt\Class_) {
|
2017-01-20 06:10:10 +01:00
|
|
|
$storage->abstract = (bool)$this->class->isAbstract();
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
foreach (ClassChecker::getInterfacesForClass(
|
|
|
|
$this->fq_class_name
|
2017-02-02 06:45:23 +01:00
|
|
|
) as $_ => $interface_name) {
|
2017-01-09 05:58:06 +01:00
|
|
|
$interface_storage = self::$storage[strtolower($interface_name)];
|
2016-12-30 18:41:14 +01:00
|
|
|
|
|
|
|
$storage->public_class_constants += $interface_storage->public_class_constants;
|
2016-10-15 06:12:57 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
foreach ($interface_storage->methods as $method_name => $method) {
|
|
|
|
if ($method->visibility === self::VISIBILITY_PUBLIC) {
|
|
|
|
$mentioned_method_id = $interface_name . '::' . $method_name;
|
|
|
|
$implemented_method_id = $this->fq_class_name . '::' . $method_name;
|
|
|
|
|
|
|
|
MethodChecker::setOverriddenMethodId($implemented_method_id, $mentioned_method_id);
|
2017-01-07 21:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 19:53:40 +01:00
|
|
|
$storage->registered = true;
|
|
|
|
|
2017-01-07 21:57:25 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Context|null $class_context
|
|
|
|
* @param Context|null $global_context
|
|
|
|
* @param bool $update_docblocks
|
|
|
|
* @return null|false
|
|
|
|
*/
|
|
|
|
public function analyze(
|
|
|
|
Context $class_context = null,
|
|
|
|
Context $global_context = null,
|
|
|
|
$update_docblocks = false
|
|
|
|
) {
|
|
|
|
$fq_class_name = $class_context && $class_context->self ? $class_context->self : $this->fq_class_name;
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[strtolower($fq_class_name)];
|
2017-01-07 21:57:25 +01:00
|
|
|
|
2017-01-23 21:36:06 +01:00
|
|
|
if (($this->class instanceof PhpParser\Node\Stmt\Class_ ||
|
|
|
|
$this->class instanceof PhpParser\Node\Stmt\Interface_
|
|
|
|
) &&
|
|
|
|
$this->parent_fq_class_name &&
|
|
|
|
!ClassLikeChecker::classOrInterfaceExists($this->parent_fq_class_name, $this->getFileChecker())
|
|
|
|
) {
|
|
|
|
// we should not normally get here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-07 21:57:25 +01:00
|
|
|
if ($this instanceof ClassChecker && $this->class instanceof PhpParser\Node\Stmt\Class_) {
|
2017-01-12 03:37:53 +01:00
|
|
|
$class_interfaces = ClassChecker::getInterfacesForClass($this->fq_class_name);
|
|
|
|
|
2017-01-16 01:02:36 +01:00
|
|
|
if (!$this->class->isAbstract()) {
|
|
|
|
foreach ($class_interfaces as $interface_id => $interface_name) {
|
|
|
|
if (!isset(self::$storage[strtolower($interface_name)])) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-01-16 01:02:36 +01:00
|
|
|
$interface_storage = self::$storage[strtolower($interface_name)];
|
|
|
|
|
|
|
|
$storage->public_class_constants += $interface_storage->public_class_constants;
|
|
|
|
|
|
|
|
foreach ($interface_storage->methods as $method_name => $method) {
|
|
|
|
if ($method->visibility === self::VISIBILITY_PUBLIC) {
|
|
|
|
$implemented_method_id = $this->fq_class_name . '::' . $method_name;
|
|
|
|
$mentioned_method_id = $interface_name . '::' . $method_name;
|
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($implemented_method_id);
|
|
|
|
|
|
|
|
$method_storage = $declaring_method_id
|
|
|
|
? MethodChecker::getStorage($declaring_method_id)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!$method_storage) {
|
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId($mentioned_method_id);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnimplementedInterfaceMethod(
|
|
|
|
'Method ' . $cased_method_id . ' is not defined on class ' .
|
|
|
|
$this->fq_class_name,
|
|
|
|
new CodeLocation($this, $this->class, true)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
} elseif ($method_storage->visibility !== self::VISIBILITY_PUBLIC) {
|
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId($mentioned_method_id);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleMethod(
|
|
|
|
'Interface-defined method ' . $cased_method_id .
|
|
|
|
' must be public in ' . $this->fq_class_name,
|
|
|
|
new CodeLocation($this, $this->class, true)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
2016-10-15 06:12:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-16 00:01:04 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!$class_context) {
|
2017-01-17 00:33:04 +01:00
|
|
|
$class_context = new Context($this->fq_class_name);
|
2017-02-01 05:24:33 +01:00
|
|
|
$class_context->count_references = $this->getFileChecker()->project_checker->count_references;
|
2017-01-07 20:35:07 +01:00
|
|
|
$class_context->parent = $this->parent_fq_class_name;
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 00:27:24 +01:00
|
|
|
if ($this->leftover_stmts) {
|
|
|
|
(new StatementsChecker($this))->analyze($this->leftover_stmts, $class_context);
|
|
|
|
}
|
|
|
|
|
2017-01-27 16:49:37 +01:00
|
|
|
$available_properties = [];
|
|
|
|
|
2017-01-28 01:57:59 +01:00
|
|
|
foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
|
|
|
|
if (explode('::', $appearing_property_id)[0] !== $fq_class_name) {
|
|
|
|
continue;
|
2017-01-27 16:49:37 +01:00
|
|
|
}
|
2017-01-19 19:11:45 +01:00
|
|
|
|
2017-01-28 01:57:59 +01:00
|
|
|
$property_class_name = self::getDeclaringClassForProperty($appearing_property_id);
|
|
|
|
$property_class_storage = self::$storage[strtolower((string)$property_class_name)];
|
|
|
|
$property_class_name = self::getDeclaringClassForProperty($appearing_property_id);
|
2017-01-19 19:11:45 +01:00
|
|
|
|
2017-01-28 01:57:59 +01:00
|
|
|
$property = $property_class_storage->properties[$property_name];
|
2017-01-19 19:11:45 +01:00
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
if ($property->type) {
|
|
|
|
$property_type = clone $property->type;
|
|
|
|
|
|
|
|
if (!$property_type->isMixed() &&
|
|
|
|
!$property->has_default &&
|
|
|
|
!$property->type->isNullable()
|
|
|
|
) {
|
|
|
|
$property_type->initialized = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$property_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if ($property->is_static) {
|
2017-01-07 20:35:07 +01:00
|
|
|
$property_id = $this->fq_class_name . '::$' . $property_name;
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
$class_context->vars_in_scope[$property_id] = $property_type;
|
2017-01-02 21:31:18 +01:00
|
|
|
} else {
|
2017-01-27 07:23:12 +01:00
|
|
|
$class_context->vars_in_scope['$this->' . $property_name] = $property_type;
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
$constructor_checker = null;
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2017-01-27 07:23:12 +01:00
|
|
|
$method_checker = $this->analyzeClassMethod(
|
|
|
|
$stmt,
|
|
|
|
$this,
|
|
|
|
$class_context,
|
|
|
|
$global_context,
|
|
|
|
$update_docblocks
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($stmt->name === '__construct') {
|
|
|
|
$constructor_checker = $method_checker;
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
|
|
|
foreach ($stmt->traits as $trait) {
|
|
|
|
$fq_trait_name = self::getFQCLNFromNameObject(
|
|
|
|
$trait,
|
|
|
|
$this->source
|
|
|
|
);
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$trait_checker = self::$trait_checkers[$fq_trait_name];
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
foreach ($trait_checker->class->stmts as $trait_stmt) {
|
|
|
|
if ($trait_stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2017-01-27 07:23:12 +01:00
|
|
|
$this->analyzeClassMethod(
|
|
|
|
$trait_stmt,
|
|
|
|
$trait_checker,
|
|
|
|
$class_context,
|
|
|
|
$global_context
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
if (!$config->excludeIssueInFile('PropertyNotSetInConstructor', $this->getFilePath())) {
|
2017-01-27 16:28:21 +01:00
|
|
|
$uninitialized_variables = [];
|
|
|
|
$uninitialized_properties = [];
|
|
|
|
|
|
|
|
foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
|
|
|
|
if (explode('::', $appearing_property_id)[0] !== $fq_class_name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_class_name = self::getDeclaringClassForProperty($appearing_property_id);
|
|
|
|
$property_class_storage = self::$storage[strtolower((string)$property_class_name)];
|
|
|
|
$property_class_name = self::getDeclaringClassForProperty($appearing_property_id);
|
|
|
|
|
|
|
|
$property = $property_class_storage->properties[$property_name];
|
2017-01-27 07:23:12 +01:00
|
|
|
|
|
|
|
if (!$property->has_default &&
|
|
|
|
$property->type &&
|
|
|
|
!$property->type->isMixed() &&
|
|
|
|
!$property->type->isNullable() &&
|
|
|
|
!$property->is_static
|
|
|
|
) {
|
2017-01-27 16:28:21 +01:00
|
|
|
$uninitialized_variables[] = '$this->' . $property_name;
|
|
|
|
$uninitialized_properties[$property_name] = $property;
|
2017-01-27 07:23:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 16:28:21 +01:00
|
|
|
if ($uninitialized_properties) {
|
2017-01-27 07:23:12 +01:00
|
|
|
if (isset($storage->methods['__construct']) && $constructor_checker) {
|
|
|
|
$method_context = clone $class_context;
|
|
|
|
$method_context->collect_initializations = true;
|
|
|
|
$method_context->vars_in_scope['$this'] = Type::parseString($fq_class_name);
|
2017-02-08 08:23:17 +01:00
|
|
|
$method_context->vars_possibly_in_scope['$this'] = true;
|
2017-01-27 07:23:12 +01:00
|
|
|
|
|
|
|
$constructor_checker->analyze($method_context, null, true);
|
|
|
|
|
2017-01-27 16:28:21 +01:00
|
|
|
foreach ($uninitialized_properties as $property_name => $property) {
|
2017-01-27 07:23:12 +01:00
|
|
|
if (!isset($method_context->vars_in_scope['$this->' . $property_name])) {
|
|
|
|
throw new \UnexpectedValueException('$this->' . $property_name . ' should be in scope');
|
|
|
|
}
|
|
|
|
|
|
|
|
$end_type = $method_context->vars_in_scope['$this->' . $property_name];
|
|
|
|
|
2017-01-27 16:28:21 +01:00
|
|
|
if (!$end_type->initialized && $property->location) {
|
2017-01-27 07:23:12 +01:00
|
|
|
$property_id = $this->fq_class_name . '::$' . $property_name;
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PropertyNotSetInConstructor(
|
|
|
|
'Property ' . $property_id . ' is not defined in constructor of ' .
|
|
|
|
$this->fq_class_name . ' or in any private methods called in the constructor',
|
2017-01-27 07:40:22 +01:00
|
|
|
$property->location
|
2017-01-27 07:23:12 +01:00
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-27 16:28:21 +01:00
|
|
|
} elseif (!$this instanceof TraitChecker) {
|
|
|
|
$first_uninitialized_property = array_shift($uninitialized_properties);
|
|
|
|
|
|
|
|
if ($first_uninitialized_property->location) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingConstructor(
|
2017-01-29 06:34:42 +01:00
|
|
|
$fq_class_name . ' has an uninitialized variable ' . $uninitialized_variables[0] .
|
2017-01-27 16:28:21 +01:00
|
|
|
', but no constructor',
|
|
|
|
$first_uninitialized_property->location
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\ClassMethod $stmt
|
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param Context $class_context
|
|
|
|
* @param Context|null $global_context
|
|
|
|
* @param boolean $update_docblocks
|
2017-01-27 07:23:12 +01:00
|
|
|
* @return MethodChecker|null
|
2017-01-12 03:37:53 +01:00
|
|
|
*/
|
|
|
|
protected function analyzeClassMethod(
|
|
|
|
PhpParser\Node\Stmt\ClassMethod $stmt,
|
|
|
|
StatementsSource $source,
|
|
|
|
Context $class_context,
|
|
|
|
Context $global_context = null,
|
|
|
|
$update_docblocks = false
|
|
|
|
) {
|
|
|
|
$config = Config::getInstance();
|
2017-01-08 18:55:32 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$method_checker = new MethodChecker($stmt, $source);
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$actual_method_id = (string)$method_checker->getMethodId();
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($class_context->self && $class_context->self !== $source->getFQCLN()) {
|
|
|
|
$analyzed_method_id = (string)$method_checker->getMethodId($class_context->self);
|
|
|
|
|
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($analyzed_method_id);
|
|
|
|
|
|
|
|
if ($actual_method_id !== $declaring_method_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_checker->analyze(
|
|
|
|
clone $class_context,
|
|
|
|
$global_context ? clone $global_context : null
|
|
|
|
);
|
|
|
|
|
2017-01-16 18:59:09 +01:00
|
|
|
if (!$config->excludeIssueInFile('InvalidReturnType', $source->getFilePath())) {
|
2017-01-16 04:09:32 +01:00
|
|
|
$return_type_location = null;
|
2017-01-12 03:37:53 +01:00
|
|
|
$secondary_return_type_location = null;
|
|
|
|
|
2017-01-16 04:09:32 +01:00
|
|
|
if ($actual_method_id) {
|
|
|
|
$return_type_location = MethodChecker::getMethodReturnTypeLocation(
|
|
|
|
$actual_method_id,
|
|
|
|
$secondary_return_type_location
|
|
|
|
);
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
|
|
|
|
$method_checker->verifyReturnType(
|
|
|
|
$update_docblocks,
|
|
|
|
MethodChecker::getMethodReturnType($actual_method_id),
|
|
|
|
$class_context->self,
|
|
|
|
$return_type_location,
|
|
|
|
$secondary_return_type_location
|
|
|
|
);
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
|
|
|
|
return $method_checker;
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method_name
|
|
|
|
* @param Context $context
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getMethodMutations(
|
|
|
|
$method_name,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
|
|
|
|
strtolower($stmt->name) === strtolower($method_name)
|
|
|
|
) {
|
2017-01-12 06:54:41 +01:00
|
|
|
$project_checker = $this->getFileChecker()->project_checker;
|
|
|
|
|
|
|
|
$method_id = $this->fq_class_name . '::' . $stmt->name;
|
|
|
|
|
|
|
|
if ($project_checker->canCache() && isset($project_checker->method_checkers[$method_id])) {
|
|
|
|
$method_checker = $project_checker->method_checkers[$method_id];
|
|
|
|
} else {
|
|
|
|
$method_checker = new MethodChecker($stmt, $this);
|
|
|
|
|
|
|
|
if ($project_checker->canCache()) {
|
|
|
|
$project_checker->method_checkers[$method_id] = $method_checker;
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
|
|
|
|
$method_checker->analyze($context, null, true);
|
2017-01-06 07:07:11 +01:00
|
|
|
} 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(
|
2017-01-06 07:07:11 +01:00
|
|
|
$trait,
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source
|
2017-01-06 07:07:11 +01:00
|
|
|
);
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$trait_checker = self::$trait_checkers[$fq_trait_name];
|
|
|
|
|
|
|
|
foreach ($trait_checker->class->stmts as $trait_stmt) {
|
|
|
|
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 07:07:11 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($context->self && $context->self !== $this->fq_class_name) {
|
|
|
|
$analyzed_method_id = (string)$method_checker->getMethodId($context->self);
|
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($analyzed_method_id);
|
|
|
|
|
|
|
|
if ($actual_method_id !== $declaring_method_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_checker->analyze($context, null, true);
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 07:07:11 +01:00
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $parent_class
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
protected function registerParentClassInfo($parent_class)
|
2016-11-02 14:24:36 +01:00
|
|
|
{
|
2016-12-04 01:11:30 +01:00
|
|
|
if (!$this->class instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
throw new \UnexpectedValueException('Cannot register parent class where none exists');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->class->extends) {
|
|
|
|
throw new \UnexpectedValueException('Cannot register parent class where none exists');
|
|
|
|
}
|
|
|
|
|
2016-11-08 01:16:51 +01:00
|
|
|
if (self::checkFullyQualifiedClassLikeName(
|
2016-11-02 14:24:36 +01:00
|
|
|
$parent_class,
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->getFileChecker(),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->class->extends, true),
|
2017-01-09 05:58:06 +01:00
|
|
|
$this->getSuppressedIssues(),
|
|
|
|
true
|
2016-11-02 14:24:36 +01:00
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-23 21:21:40 +01:00
|
|
|
// if we don't care the parent class does not exist, exit now.
|
|
|
|
if (!ClassLikeChecker::classOrInterfaceExists($parent_class, $this->getFileChecker())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
self::$class_extends[$this->fq_class_name] = self::$class_extends[$this->parent_fq_class_name];
|
|
|
|
self::$class_extends[$this->fq_class_name][$this->parent_fq_class_name] = true;
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-02 01:50:29 +01:00
|
|
|
$this->registerInheritedMethods($this->fq_class_name, $parent_class);
|
|
|
|
$this->registerInheritedProperties($this->fq_class_name, $parent_class);
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[strtolower($this->fq_class_name)];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$parent_storage = self::$storage[strtolower($parent_class)];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->class_implements += $parent_storage->class_implements;
|
2016-11-04 01:51:56 +01:00
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants = $parent_storage->public_class_constants;
|
|
|
|
$storage->protected_class_constants = $parent_storage->protected_class_constants;
|
2016-11-04 01:51:56 +01:00
|
|
|
|
2017-01-15 01:34:10 +01:00
|
|
|
$storage->parent_classes = array_merge([strtolower($parent_class)], $parent_storage->parent_classes);
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->used_traits = $parent_storage->used_traits;
|
2016-12-12 19:57:45 +01:00
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
FileChecker::addFileInheritanceToClass(
|
2017-01-17 00:33:04 +01:00
|
|
|
$this->source->getFilePath(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$parent_class
|
|
|
|
);
|
2017-01-02 01:50:29 +01:00
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
return null;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
/**
|
2016-11-05 01:10:59 +01:00
|
|
|
* @param PhpParser\Node\Stmt\ClassMethod $stmt
|
|
|
|
* @param Context $class_context
|
|
|
|
* @return void
|
2016-11-04 01:51:56 +01:00
|
|
|
*/
|
|
|
|
protected function visitClassMethod(
|
|
|
|
PhpParser\Node\Stmt\ClassMethod $stmt,
|
2017-01-02 21:31:18 +01:00
|
|
|
Context $class_context
|
2016-11-04 01:51:56 +01:00
|
|
|
) {
|
2017-01-06 07:07:11 +01:00
|
|
|
$method_name = strtolower($stmt->name);
|
|
|
|
$method_id = $this->fq_class_name . '::' . $method_name;
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_storage = self::$storage[strtolower($this->fq_class_name)];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
if (!isset($class_storage->methods[$method_name])) {
|
2017-02-02 06:45:23 +01:00
|
|
|
FunctionLikeChecker::register($stmt, $this);
|
2017-01-06 07:07:11 +01:00
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2016-12-29 06:32:12 +01:00
|
|
|
if (!$stmt->isAbstract() && $class_context->self) {
|
2016-12-31 17:49:04 +01:00
|
|
|
$implemented_method_id =
|
|
|
|
$class_context->self . '::' . strtolower($this->getMappedMethodName(strtolower($stmt->name)));
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
MethodChecker::setDeclaringMethodId(
|
2016-12-31 17:49:04 +01:00
|
|
|
$implemented_method_id,
|
2016-11-02 07:29:00 +01:00
|
|
|
$method_id
|
|
|
|
);
|
2016-12-31 17:49:04 +01:00
|
|
|
|
|
|
|
// set a different apparent method if we're in a trait checker
|
|
|
|
MethodChecker::setAppearingMethodId(
|
|
|
|
$implemented_method_id,
|
|
|
|
$this instanceof TraitChecker ? $implemented_method_id : $method_id
|
|
|
|
);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\TraitUse $stmt
|
|
|
|
* @param Context $class_context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
protected function visitTraitUse(
|
|
|
|
PhpParser\Node\Stmt\TraitUse $stmt,
|
2017-01-02 21:31:18 +01:00
|
|
|
Context $class_context
|
2016-11-04 01:51:56 +01:00
|
|
|
) {
|
2016-11-02 14:24:36 +01:00
|
|
|
$method_map = [];
|
|
|
|
|
|
|
|
foreach ($stmt->adaptations as $adaptation) {
|
|
|
|
if ($adaptation instanceof PhpParser\Node\Stmt\TraitUseAdaptation\Alias) {
|
|
|
|
if ($adaptation->method && $adaptation->newName) {
|
|
|
|
$method_map[strtolower($adaptation->method)] = strtolower($adaptation->newName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->traits as $trait) {
|
2016-11-08 01:16:51 +01:00
|
|
|
$trait_name = self::getFQCLNFromNameObject(
|
2016-11-02 07:29:00 +01:00
|
|
|
$trait,
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!TraitChecker::traitExists($trait_name, $this->getFileChecker())) {
|
2016-11-02 14:24:36 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-12-04 01:11:30 +01:00
|
|
|
new UndefinedTrait(
|
|
|
|
'Trait ' . $trait_name . ' does not exist',
|
|
|
|
new CodeLocation($this, $trait)
|
|
|
|
),
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source->getSuppressedIssues()
|
2016-11-02 14:24:36 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-30 05:44:05 +01:00
|
|
|
if (!TraitChecker::hasCorrectCase($trait_name, $this->getFileChecker())) {
|
2016-11-02 14:24:36 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-04 01:51:56 +01:00
|
|
|
new UndefinedTrait(
|
|
|
|
'Trait ' . $trait_name . ' has wrong casing',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $trait)
|
2016-11-04 01:51:56 +01:00
|
|
|
),
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source->getSuppressedIssues()
|
2016-11-02 14:24:36 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$trait_checker = self::$trait_checkers[$trait_name];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
|
|
|
$trait_checker->setMethodMap($method_map);
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
$trait_checker->visit($class_context);
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
self::registerInheritedProperties($this->fq_class_name, $trait_name);
|
|
|
|
|
2016-12-12 19:50:46 +01:00
|
|
|
ClassLikeChecker::registerTraitUse($this->fq_class_name, $trait_name);
|
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
FileChecker::addFileInheritanceToClass(
|
2017-01-17 00:33:04 +01:00
|
|
|
$this->source->getFilePath(),
|
2016-11-05 01:49:04 +01:00
|
|
|
$trait_name
|
2016-11-04 01:51:56 +01:00
|
|
|
);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 01:51:56 +01:00
|
|
|
|
|
|
|
return null;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
/**
|
2016-11-05 01:10:59 +01:00
|
|
|
* @param PhpParser\Node\Stmt\Property $stmt
|
|
|
|
* @param Context $class_context
|
|
|
|
* @param Config $config
|
2017-01-02 21:31:18 +01:00
|
|
|
* @return void
|
2016-11-04 01:51:56 +01:00
|
|
|
*/
|
|
|
|
protected function visitPropertyDeclaration(
|
|
|
|
PhpParser\Node\Stmt\Property $stmt,
|
|
|
|
Context $class_context,
|
2017-01-02 21:31:18 +01:00
|
|
|
Config $config
|
2016-11-04 01:51:56 +01:00
|
|
|
) {
|
2016-11-02 14:24:36 +01:00
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
$type_in_comment = null;
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[strtolower($this->fq_class_name)];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
|
|
|
if ($comment && $config->use_docblock_types) {
|
2016-11-21 05:31:10 +01:00
|
|
|
try {
|
|
|
|
$type_in_comment = CommentChecker::getTypeFromComment((string) $comment, null, $this);
|
2016-12-04 01:11:30 +01:00
|
|
|
} catch (DocblockParseException $e) {
|
2016-11-21 05:31:10 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
2016-11-21 05:45:10 +01:00
|
|
|
(string)$e->getMessage(),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $this->class, true)
|
2016-11-21 05:31:10 +01:00
|
|
|
)
|
|
|
|
)) {
|
2017-01-02 21:31:18 +01:00
|
|
|
// fall through
|
2016-11-21 05:31:10 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
} elseif (!$comment) {
|
2016-11-02 14:24:36 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
2016-11-07 23:29:51 +01:00
|
|
|
'Property ' . $this->fq_class_name . '::$' . $stmt->props[0]->name . ' does not have a ' .
|
2016-11-04 01:51:56 +01:00
|
|
|
'declared type',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this, $stmt)
|
2016-11-02 14:24:36 +01:00
|
|
|
),
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source->getSuppressedIssues()
|
2016-11-02 14:24:36 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$property_group_type = $type_in_comment ?: null;
|
2016-11-02 14:24:36 +01:00
|
|
|
|
|
|
|
foreach ($stmt->props as $property) {
|
|
|
|
if (!$property_group_type) {
|
2016-12-05 00:43:07 +01:00
|
|
|
if (!$property->default || !$config->use_property_default_for_type) {
|
2016-11-02 14:24:36 +01:00
|
|
|
$property_type = false;
|
|
|
|
} else {
|
|
|
|
$property_type = StatementsChecker::getSimpleType($property->default) ?: Type::getMixed();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$property_type = count($stmt->props) === 1 ? $property_group_type : clone $property_group_type;
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->properties[$property->name] = new PropertyStorage();
|
|
|
|
$storage->properties[$property->name]->is_static = (bool)$stmt->isStatic();
|
|
|
|
$storage->properties[$property->name]->type = $property_type;
|
2017-01-27 07:40:22 +01:00
|
|
|
$storage->properties[$property->name]->location = new CodeLocation($this, $property);
|
2017-01-27 07:23:12 +01:00
|
|
|
$storage->properties[$property->name]->has_default = $property->default ? true : false;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
if ($stmt->isPublic()) {
|
|
|
|
$storage->properties[$property->name]->visibility = self::VISIBILITY_PUBLIC;
|
|
|
|
} elseif ($stmt->isProtected()) {
|
|
|
|
$storage->properties[$property->name]->visibility = self::VISIBILITY_PROTECTED;
|
|
|
|
} elseif ($stmt->isPrivate()) {
|
|
|
|
$storage->properties[$property->name]->visibility = self::VISIBILITY_PRIVATE;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$property_id = $this->fq_class_name . '::$' . $property->name;
|
|
|
|
|
|
|
|
$implemented_property_id = $class_context->self . '::$' . $property->name;
|
|
|
|
|
|
|
|
$storage->declaring_property_ids[$property->name] = $property_id;
|
|
|
|
$storage->appearing_property_ids[$property->name] = $this instanceof TraitChecker
|
|
|
|
? $implemented_property_id
|
|
|
|
: $property_id;
|
2017-01-19 19:11:45 +01:00
|
|
|
|
|
|
|
if (!$stmt->isPrivate()) {
|
|
|
|
$storage->inheritable_property_ids[$property->name] = $property_id;
|
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\ClassConst $stmt
|
|
|
|
* @param Context $class_context
|
|
|
|
* @param Config $config
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function visitClassConstDeclaration(
|
|
|
|
PhpParser\Node\Stmt\ClassConst $stmt,
|
|
|
|
Context $class_context,
|
|
|
|
Config $config
|
|
|
|
) {
|
2016-11-02 14:24:36 +01:00
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
$type_in_comment = null;
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[strtolower((string)$class_context->self)];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
|
|
|
if ($comment && $config->use_docblock_types && count($stmt->consts) === 1) {
|
|
|
|
$type_in_comment = CommentChecker::getTypeFromComment((string) $comment, null, $this);
|
|
|
|
}
|
|
|
|
|
|
|
|
$const_type = $type_in_comment ? $type_in_comment : Type::getMixed();
|
|
|
|
|
|
|
|
foreach ($stmt->consts as $const) {
|
2016-12-04 07:44:33 +01:00
|
|
|
if ($stmt->isProtected()) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->protected_class_constants[$const->name] = $const_type;
|
2016-12-04 07:44:33 +01:00
|
|
|
} elseif ($stmt->isPrivate()) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->private_class_constants[$const->name] = $const_type;
|
2016-12-04 07:44:33 +01:00
|
|
|
} else {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants[$const->name] = $const_type;
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 06:31:29 +02:00
|
|
|
/**
|
2016-09-09 15:13:41 +02:00
|
|
|
* Check whether a class/interface exists
|
|
|
|
*
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param FileChecker $file_checker
|
2016-07-25 06:31:29 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function classOrInterfaceExists(
|
|
|
|
$fq_class_name,
|
|
|
|
FileChecker $file_checker
|
|
|
|
) {
|
|
|
|
return ClassChecker::classExists($fq_class_name, $file_checker) ||
|
|
|
|
InterfaceChecker::interfaceExists($fq_class_name, $file_checker);
|
2016-07-25 05:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param string $possible_parent
|
2016-07-25 05:38:52 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function classExtendsOrImplements(
|
|
|
|
$fq_class_name,
|
|
|
|
$possible_parent
|
|
|
|
) {
|
|
|
|
return ClassChecker::classExtends($fq_class_name, $possible_parent) ||
|
|
|
|
ClassChecker::classImplements($fq_class_name, $possible_parent);
|
2016-07-25 05:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param FileChecker $file_checker
|
|
|
|
* @param CodeLocation $code_location
|
2016-11-05 01:10:59 +01:00
|
|
|
* @param array<string> $suppressed_issues
|
2017-01-09 05:58:06 +01:00
|
|
|
* @param bool $visit_file
|
2016-07-25 05:38:52 +02:00
|
|
|
* @return bool|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public static function checkFullyQualifiedClassLikeName(
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2017-01-02 21:31:18 +01:00
|
|
|
FileChecker $file_checker,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location,
|
2017-01-09 05:58:06 +01:00
|
|
|
array $suppressed_issues,
|
|
|
|
$visit_file = false
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-11-07 23:29:51 +01:00
|
|
|
if (empty($fq_class_name)) {
|
2016-04-12 17:59:27 +02:00
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name);
|
2016-03-17 19:06:01 +01:00
|
|
|
|
2016-12-05 03:04:25 +01:00
|
|
|
if (in_array($fq_class_name, ['callable', 'iterable'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$class_exists = ClassChecker::classExists($fq_class_name, $file_checker);
|
|
|
|
$interface_exists = InterfaceChecker::interfaceExists($fq_class_name, $file_checker);
|
2016-08-14 00:54:49 +02:00
|
|
|
|
|
|
|
if (!$class_exists && !$interface_exists) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new UndefinedClass(
|
2016-11-07 23:29:51 +01:00
|
|
|
'Class or interface ' . $fq_class_name . ' does not exist',
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-07-24 23:06:36 +02:00
|
|
|
$suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 23:06:36 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 06:23:17 +01:00
|
|
|
if (($class_exists && !ClassChecker::hasCorrectCasing($fq_class_name, $file_checker)) ||
|
2017-01-09 05:58:06 +01:00
|
|
|
($interface_exists && !InterfaceChecker::hasCorrectCasing($fq_class_name, $file_checker))
|
2016-08-14 00:54:49 +02:00
|
|
|
) {
|
2017-02-01 05:24:33 +01:00
|
|
|
$file_checker->evaluateClassLike($fq_class_name);
|
2017-01-18 06:33:48 +01:00
|
|
|
|
|
|
|
if (ClassLikeChecker::isUserDefined($fq_class_name)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidClass(
|
|
|
|
'Class or interface ' . $fq_class_name . ' has wrong casing',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-10 07:09:47 +02:00
|
|
|
}
|
2016-03-17 19:06:01 +01:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
FileChecker::addFileReferenceToClass(
|
2017-01-17 00:33:04 +01:00
|
|
|
$code_location->file_path,
|
2016-12-08 04:38:57 +01:00
|
|
|
$fq_class_name
|
|
|
|
);
|
2016-10-05 01:23:38 +02:00
|
|
|
|
2016-07-24 23:06:36 +02:00
|
|
|
return true;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* Gets the fully-qualified class name from a Name object
|
2016-10-09 23:54:58 +02:00
|
|
|
*
|
2016-12-24 19:23:22 +01:00
|
|
|
* @param PhpParser\Node\Name $class_name
|
2017-01-07 20:35:07 +01:00
|
|
|
* @param StatementsSource $source
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public static function getFQCLNFromNameObject(
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Name $class_name,
|
2017-01-07 20:35:07 +01:00
|
|
|
StatementsSource $source
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2017-01-06 07:07:11 +01:00
|
|
|
if ($class_name->parts == ['self']) {
|
|
|
|
return 'self';
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return implode('\\', $class_name->parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
return self::getFQCLNFromString(
|
|
|
|
implode('\\', $class_name->parts),
|
2017-01-07 20:35:07 +01:00
|
|
|
$source
|
2017-01-06 07:07:11 +01:00
|
|
|
);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
2016-11-05 22:53:30 +01:00
|
|
|
* @param string $class
|
2017-01-07 20:35:07 +01:00
|
|
|
* @param StatementsSource $source
|
2016-10-12 07:38:29 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
public static function getFQCLNFromString($class, StatementsSource $source)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-04-12 17:59:27 +02:00
|
|
|
if (empty($class)) {
|
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($class[0] === '\\') {
|
2016-02-18 21:05:13 +01:00
|
|
|
return substr($class, 1);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
$imported_namespaces = $source->getAliasedClasses();
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
if (strpos($class, '\\') !== false) {
|
|
|
|
$class_parts = explode('\\', $class);
|
|
|
|
$first_namespace = array_shift($class_parts);
|
|
|
|
|
2016-09-10 05:17:56 +02:00
|
|
|
if (isset($imported_namespaces[strtolower($first_namespace)])) {
|
|
|
|
return $imported_namespaces[strtolower($first_namespace)] . '\\' . implode('\\', $class_parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-09-10 05:17:56 +02:00
|
|
|
} elseif (isset($imported_namespaces[strtolower($class)])) {
|
|
|
|
return $imported_namespaces[strtolower($class)];
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
$namespace = $source->getNamespace();
|
|
|
|
|
2016-02-27 01:11:11 +01:00
|
|
|
return ($namespace ? $namespace . '\\' : '') . $class;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getNamespace()
|
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
return $this->source->getNamespace();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getAliasedClassesFlipped()
|
|
|
|
{
|
2016-11-13 17:24:46 +01:00
|
|
|
if ($this->source instanceof NamespaceChecker || $this->source instanceof FileChecker) {
|
|
|
|
return $this->source->getAliasedClassesFlipped();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
2016-11-13 00:51:48 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public function getFQCLN()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-11-07 23:29:51 +01:00
|
|
|
return $this->fq_class_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-04 04:41:45 +01:00
|
|
|
* @return string|null
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->class->name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
public function getParentFQCLN()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
return $this->parent_fq_class_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-30 00:48:09 +01:00
|
|
|
public function hasCustomGet()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->has_custom_get;
|
2016-01-30 00:48:09 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param ReflectionClass $reflected_class
|
2017-01-09 05:58:06 +01:00
|
|
|
* @param ProjectChecker $project_checker
|
2016-11-02 14:24:36 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function registerReflectedClass(
|
|
|
|
$class_name,
|
2017-01-09 05:58:06 +01:00
|
|
|
ReflectionClass $reflected_class,
|
|
|
|
ProjectChecker $project_checker
|
2017-01-02 21:31:18 +01:00
|
|
|
) {
|
2017-01-02 01:50:29 +01:00
|
|
|
$class_name = $reflected_class->name;
|
|
|
|
|
|
|
|
if ($class_name === 'LibXMLError') {
|
|
|
|
$class_name = 'libXMLError';
|
|
|
|
}
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_name_lower = strtolower($class_name);
|
|
|
|
|
|
|
|
if (isset(self::$storage[$class_name_lower]) && self::$storage[$class_name_lower]->reflected) {
|
2016-12-30 21:53:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:50:29 +01:00
|
|
|
$reflected_parent_class = $reflected_class->getParentClass();
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[$class_name_lower] = new ClassLikeStorage();
|
2017-02-08 06:28:26 +01:00
|
|
|
$storage->name = $class_name;
|
2017-01-20 06:10:10 +01:00
|
|
|
$storage->abstract = $reflected_class->isAbstract();
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-01-02 01:50:29 +01:00
|
|
|
self::$class_extends[$class_name] = [];
|
2016-12-30 18:41:14 +01:00
|
|
|
|
2017-01-02 01:50:29 +01:00
|
|
|
if ($reflected_parent_class) {
|
|
|
|
$parent_class_name = $reflected_parent_class->getName();
|
2017-01-09 05:58:06 +01:00
|
|
|
self::registerReflectedClass($parent_class_name, $reflected_parent_class, $project_checker);
|
2016-10-27 22:05:27 +02:00
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$parent_storage = self::$storage[strtolower($parent_class_name)];
|
2017-01-02 01:50:29 +01:00
|
|
|
|
|
|
|
self::$class_extends[$class_name] = self::$class_extends[$parent_class_name];
|
|
|
|
self::$class_extends[$class_name][$parent_class_name] = true;
|
|
|
|
|
|
|
|
self::registerInheritedMethods($class_name, $parent_class_name);
|
|
|
|
self::registerInheritedProperties($class_name, $parent_class_name);
|
|
|
|
|
|
|
|
$storage->class_implements = $parent_storage->class_implements;
|
|
|
|
|
|
|
|
$storage->public_class_constants = $parent_storage->public_class_constants;
|
|
|
|
$storage->protected_class_constants = $parent_storage->protected_class_constants;
|
2017-01-15 01:34:10 +01:00
|
|
|
$storage->parent_classes = array_merge([strtolower($parent_class_name)], $parent_storage->parent_classes);
|
2017-01-02 01:50:29 +01:00
|
|
|
|
|
|
|
$storage->used_traits = $parent_storage->used_traits;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-10-27 22:05:27 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
$class_properties = $reflected_class->getProperties();
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$public_mapped_properties = self::inPropertyMap($class_name)
|
|
|
|
? self::getPropertyMap()[strtolower($class_name)]
|
|
|
|
: [];
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
/** @var \ReflectionProperty $class_property */
|
|
|
|
foreach ($class_properties as $class_property) {
|
2017-01-02 01:09:17 +01:00
|
|
|
$property_name = $class_property->getName();
|
|
|
|
$storage->properties[$property_name] = new PropertyStorage();
|
|
|
|
|
|
|
|
$storage->properties[$property_name]->type = Type::getMixed();
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($class_property->isStatic()) {
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->properties[$property_name]->is_static = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class_property->isPublic()) {
|
|
|
|
$storage->properties[$property_name]->visibility = self::VISIBILITY_PUBLIC;
|
|
|
|
} elseif ($class_property->isProtected()) {
|
|
|
|
$storage->properties[$property_name]->visibility = self::VISIBILITY_PROTECTED;
|
|
|
|
} elseif ($class_property->isPrivate()) {
|
|
|
|
$storage->properties[$property_name]->visibility = self::VISIBILITY_PRIVATE;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$property_id = (string)$class_property->class . '::$' . $property_name;
|
|
|
|
|
|
|
|
$storage->declaring_property_ids[$property_name] = $property_id;
|
|
|
|
$storage->appearing_property_ids[$property_name] = $property_id;
|
2017-01-19 19:11:45 +01:00
|
|
|
|
|
|
|
if (!$class_property->isPrivate()) {
|
|
|
|
$storage->inheritable_property_ids[$property_name] = $property_id;
|
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// have to do this separately as there can be new properties here
|
|
|
|
foreach ($public_mapped_properties as $property_name => $type) {
|
|
|
|
if (!isset($storage->properties[$property_name])) {
|
|
|
|
$storage->properties[$property_name] = new PropertyStorage();
|
|
|
|
$storage->properties[$property_name]->visibility = self::VISIBILITY_PUBLIC;
|
2016-10-27 22:05:27 +02:00
|
|
|
|
2017-01-02 01:50:29 +01:00
|
|
|
$property_id = $class_name . '::$' . $property_name;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$storage->declaring_property_ids[$property_name] = $property_id;
|
|
|
|
$storage->appearing_property_ids[$property_name] = $property_id;
|
2017-01-19 19:11:45 +01:00
|
|
|
$storage->inheritable_property_ids[$property_name] = $property_id;
|
2016-10-27 22:05:27 +02:00
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$storage->properties[$property_name]->type = Type::parseString($type);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-10-27 22:05:27 +02:00
|
|
|
|
2016-12-17 01:22:30 +01:00
|
|
|
/** @var array<string, int|string|float|null|array> */
|
2016-11-02 14:24:36 +01:00
|
|
|
$class_constants = $reflected_class->getConstants();
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
foreach ($class_constants as $name => $value) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants[$name] = self::getTypeFromValue($value);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-08-14 18:06:53 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if ($reflected_class->isInterface()) {
|
2017-01-09 05:58:06 +01:00
|
|
|
$project_checker->addFullyQualifiedInterfaceName($class_name);
|
|
|
|
} elseif ($reflected_class->isTrait()) {
|
|
|
|
$project_checker->addFullyQualifiedTraitName($class_name);
|
|
|
|
} else {
|
|
|
|
$project_checker->addFullyQualifiedClassName($class_name);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$storage->registered = true;
|
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
$reflection_methods = $reflected_class->getMethods(
|
|
|
|
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
|
|
|
|
);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$interfaces = $reflected_class->getInterfaces();
|
|
|
|
|
|
|
|
/** @var \ReflectionClass $interface */
|
|
|
|
foreach ($interfaces as $interface) {
|
|
|
|
$interface_name = $interface->getName();
|
2017-01-09 05:58:06 +01:00
|
|
|
self::registerReflectedClass($interface_name, $interface, $project_checker);
|
2017-01-17 02:47:23 +01:00
|
|
|
|
|
|
|
if ($reflected_class->isInterface()) {
|
|
|
|
$storage->parent_interfaces[] = $interface_name;
|
|
|
|
} else {
|
|
|
|
$storage->class_implements[strtolower($interface_name)] = $interface_name;
|
|
|
|
}
|
2016-12-30 18:41:14 +01:00
|
|
|
}
|
|
|
|
|
2016-11-21 04:40:19 +01:00
|
|
|
/** @var \ReflectionMethod $reflection_method */
|
2016-11-02 14:24:36 +01:00
|
|
|
foreach ($reflection_methods as $reflection_method) {
|
|
|
|
MethodChecker::extractReflectionMethodInfo($reflection_method);
|
2016-08-15 06:31:34 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($reflection_method->class !== $class_name) {
|
|
|
|
MethodChecker::setDeclaringMethodId(
|
2016-12-24 12:03:55 +01:00
|
|
|
$class_name . '::' . strtolower($reflection_method->name),
|
|
|
|
$reflection_method->class . '::' . strtolower($reflection_method->name)
|
2016-11-02 14:24:36 +01:00
|
|
|
);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
MethodChecker::setAppearingMethodId(
|
|
|
|
$class_name . '::' . strtolower($reflection_method->name),
|
|
|
|
$reflection_method->class . '::' . strtolower($reflection_method->name)
|
|
|
|
);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
continue;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-01-02 01:50:29 +01:00
|
|
|
* @param string $fq_class_name
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param string $parent_class
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:50:29 +01:00
|
|
|
protected static function registerInheritedMethods($fq_class_name, $parent_class)
|
2016-08-15 06:31:34 +02:00
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
$parent_storage = self::$storage[strtolower($parent_class)];
|
|
|
|
$storage = self::$storage[strtolower($fq_class_name)];
|
2016-12-12 20:29:58 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// register where they appear (can never be in a trait)
|
|
|
|
foreach ($parent_storage->appearing_method_ids as $method_name => $appearing_method_id) {
|
2016-12-12 20:29:58 +01:00
|
|
|
$parent_method_id = $parent_class . '::' . $method_name;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2016-12-31 17:49:04 +01:00
|
|
|
/** @var string */
|
|
|
|
$appearing_method_id = MethodChecker::getAppearingMethodId($parent_method_id);
|
2017-01-02 01:50:29 +01:00
|
|
|
$implemented_method_id = $fq_class_name . '::' . $method_name;
|
2016-12-12 20:29:58 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->appearing_method_ids[$method_name] = $appearing_method_id;
|
2016-12-12 20:29:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// register where they're declared
|
|
|
|
foreach ($parent_storage->declaring_method_ids as $method_name => $declaring_method_id) {
|
2016-08-15 20:20:06 +02:00
|
|
|
$parent_method_id = $parent_class . '::' . $method_name;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2016-11-07 05:29:54 +01:00
|
|
|
/** @var string */
|
2016-10-15 06:12:57 +02:00
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($parent_method_id);
|
2017-01-02 01:50:29 +01:00
|
|
|
$implemented_method_id = $fq_class_name . '::' . $method_name;
|
2016-08-15 06:31:34 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->declaring_method_ids[$method_name] = $declaring_method_id;
|
|
|
|
|
|
|
|
MethodChecker::setOverriddenMethodId($implemented_method_id, $declaring_method_id);
|
2016-08-15 06:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
2017-01-02 01:50:29 +01:00
|
|
|
* @param string $fq_class_name
|
2017-01-02 01:09:17 +01:00
|
|
|
* @param string $parent_class
|
|
|
|
* @return void
|
2016-10-12 07:38:29 +02:00
|
|
|
*/
|
2017-01-02 01:50:29 +01:00
|
|
|
protected static function registerInheritedProperties($fq_class_name, $parent_class)
|
2016-08-07 02:27:13 +02:00
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
$parent_storage = self::$storage[strtolower($parent_class)];
|
|
|
|
$storage = self::$storage[strtolower($fq_class_name)];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// register where they appear (can never be in a trait)
|
|
|
|
foreach ($parent_storage->appearing_property_ids as $property_name => $appearing_property_id) {
|
|
|
|
$storage->appearing_property_ids[$property_name] = $appearing_property_id;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// register where they're declared
|
|
|
|
foreach ($parent_storage->declaring_property_ids as $property_name => $declaring_property_id) {
|
|
|
|
$storage->declaring_property_ids[$property_name] = $declaring_property_id;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2017-01-19 19:11:45 +01:00
|
|
|
|
|
|
|
// register where they're declared
|
|
|
|
foreach ($parent_storage->inheritable_property_ids as $property_name => $inheritable_property_id) {
|
|
|
|
$storage->inheritable_property_ids[$property_name] = $inheritable_property_id;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param mixed $visibility
|
2017-01-02 01:09:17 +01:00
|
|
|
* @param bool $is_static
|
|
|
|
* @return array<string, PropertyStorage>
|
2016-10-12 07:38:29 +02:00
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
public static function getPropertiesForClass($class_name, $visibility, $is_static)
|
2016-08-07 02:27:13 +02:00
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_name = strtolower($class_name);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!isset(self::$storage[$class_name])) {
|
|
|
|
throw new \UnexpectedValueException('$storage should not be null for ' . $class_name);
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage = self::$storage[$class_name];
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$properties = [];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
foreach ($storage->properties as $property_name => $property) {
|
|
|
|
if (!$property->is_static) {
|
|
|
|
if ($visibility === ReflectionProperty::IS_PRIVATE ||
|
|
|
|
$property->visibility === ClassLikeChecker::VISIBILITY_PUBLIC ||
|
|
|
|
($property->visibility === ClassLikeChecker::VISIBILITY_PROTECTED &&
|
|
|
|
$visibility === ReflectionProperty::IS_PROTECTED)
|
|
|
|
) {
|
|
|
|
$properties[$property_name] = $property;
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2016-07-23 16:58:53 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
return $properties;
|
2016-01-30 00:48:09 +01:00
|
|
|
}
|
2016-04-12 01:13:50 +02:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
/**
|
|
|
|
* Gets the Psalm type from a particular value
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param mixed $visibility
|
|
|
|
* @return array<string,Type\Union>
|
|
|
|
*/
|
2016-08-13 20:20:46 +02:00
|
|
|
public static function getConstantsForClass($class_name, $visibility)
|
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_name = strtolower($class_name);
|
|
|
|
|
|
|
|
$class_name = strtolower($class_name);
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!isset(self::$storage[$class_name])) {
|
|
|
|
throw new \UnexpectedValueException('$storage should not be null for ' . $class_name);
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage = self::$storage[$class_name];
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
2016-12-30 18:41:14 +01:00
|
|
|
return $storage->public_class_constants;
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 07:44:33 +01:00
|
|
|
if ($visibility === ReflectionProperty::IS_PROTECTED) {
|
|
|
|
return array_merge(
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants,
|
|
|
|
$storage->protected_class_constants
|
2016-12-04 07:44:33 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($visibility === ReflectionProperty::IS_PRIVATE) {
|
|
|
|
return array_merge(
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants,
|
|
|
|
$storage->protected_class_constants,
|
|
|
|
$storage->private_class_constants
|
2016-12-04 07:44:33 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException('Must specify $visibility');
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param string $const_name
|
|
|
|
* @param Type\Union $type
|
2016-12-04 07:44:33 +01:00
|
|
|
* @param int $visibility
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-04 07:44:33 +01:00
|
|
|
public static function setConstantType($class_name, $const_name, Type\Union $type, $visibility)
|
2016-08-13 20:20:46 +02:00
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[strtolower($class_name)];
|
2016-12-30 18:41:14 +01:00
|
|
|
|
2016-12-04 07:44:33 +01:00
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants[$const_name] = $type;
|
2016-12-04 07:44:33 +01:00
|
|
|
} elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->protected_class_constants[$const_name] = $type;
|
2016-12-04 07:44:33 +01:00
|
|
|
} elseif ($visibility === ReflectionProperty::IS_PRIVATE) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->private_class_constants[$const_name] = $type;
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
/**
|
|
|
|
* Whether or not a given property exists
|
|
|
|
*
|
|
|
|
* @param string $property_id
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function propertyExists($property_id)
|
|
|
|
{
|
|
|
|
// remove trailing backslash if it exists
|
|
|
|
$property_id = preg_replace('/^\\\\/', '', $property_id);
|
|
|
|
|
|
|
|
list($fq_class_name, $property_name) = explode('::$', $property_id);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
if (!isset(self::$storage[strtolower($fq_class_name)])) {
|
2017-01-02 21:31:18 +01:00
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Storage not defined for ' . $property_id
|
|
|
|
);
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_storage = self::$storage[strtolower($fq_class_name)];
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
if (isset($class_storage->declaring_property_ids[$property_name])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $property_id
|
|
|
|
* @param string|null $calling_context
|
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param CodeLocation $code_location
|
|
|
|
* @param array $suppressed_issues
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$calling_context,
|
|
|
|
StatementsSource $source,
|
|
|
|
CodeLocation $code_location,
|
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
|
|
|
$declaring_property_class = self::getDeclaringClassForProperty($property_id);
|
|
|
|
$appearing_property_class = self::getAppearingClassForProperty($property_id);
|
|
|
|
|
|
|
|
if (!$declaring_property_class || !$appearing_property_class) {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Appearing/Declaring classes are not defined for ' . $property_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
list($property_class, $property_name) = explode('::$', (string)$property_id);
|
|
|
|
|
|
|
|
// if the calling class is the same, we know the property exists, so it must be visible
|
|
|
|
if ($appearing_property_class === $calling_context) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($source->getSource() instanceof TraitChecker && $declaring_property_class === $source->getFQCLN()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_storage = self::$storage[strtolower($declaring_property_class)];
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
if (!$class_storage) {
|
|
|
|
throw new \UnexpectedValueException('$class_storage should not be null for ' . $declaring_property_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage = $class_storage->properties[$property_name];
|
|
|
|
|
|
|
|
if (!$storage) {
|
|
|
|
throw new \UnexpectedValueException('$storage should not be null for ' . $property_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($storage->visibility) {
|
|
|
|
case self::VISIBILITY_PUBLIC:
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case self::VISIBILITY_PRIVATE:
|
|
|
|
if (!$calling_context || $appearing_property_class !== $calling_context) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access private property ' . $property_id . ' from context ' . $calling_context,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case self::VISIBILITY_PROTECTED:
|
|
|
|
if ($appearing_property_class === $calling_context) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$calling_context) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access protected property ' . $property_id,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassChecker::classExtends($appearing_property_class, $calling_context)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ClassChecker::classExtends($calling_context, $appearing_property_class)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access protected property ' . $property_id . ' from context ' . $calling_context,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $property_id
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function getDeclaringClassForProperty($property_id)
|
|
|
|
{
|
|
|
|
list($fq_class_name, $property_name) = explode('::$', $property_id);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$fq_class_name = strtolower($fq_class_name);
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
if (isset(ClassLikeChecker::$storage[$fq_class_name]->declaring_property_ids[$property_name])) {
|
|
|
|
$declaring_property_id = ClassLikeChecker::$storage[$fq_class_name]->declaring_property_ids[$property_name];
|
|
|
|
|
|
|
|
return explode('::$', $declaring_property_id)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the class this property appears in (vs is declared in, which could give a trait)
|
|
|
|
*
|
|
|
|
* @param string $property_id
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function getAppearingClassForProperty($property_id)
|
|
|
|
{
|
|
|
|
list($fq_class_name, $property_name) = explode('::$', $property_id);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$fq_class_name = strtolower($fq_class_name);
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
if (isset(ClassLikeChecker::$storage[$fq_class_name]->appearing_property_ids[$property_name])) {
|
|
|
|
$appearing_property_id = ClassLikeChecker::$storage[$fq_class_name]->appearing_property_ids[$property_name];
|
|
|
|
|
|
|
|
return explode('::$', $appearing_property_id)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $method_name
|
2016-12-24 12:03:55 +01:00
|
|
|
* @return string
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-08-15 20:20:06 +02:00
|
|
|
protected function getMappedMethodName($method_name)
|
|
|
|
{
|
|
|
|
return $method_name;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $file_path
|
2016-12-17 00:56:23 +01:00
|
|
|
* @return array<string>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function getClassesForFile($file_path)
|
2016-10-05 01:23:38 +02:00
|
|
|
{
|
2017-01-02 21:31:18 +01:00
|
|
|
return isset(self::$file_classes[$file_path]) ? array_unique(self::$file_classes[$file_path]) : [];
|
2016-10-05 01:23:38 +02:00
|
|
|
}
|
|
|
|
|
2016-10-30 16:14:36 +01:00
|
|
|
/**
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2016-10-30 16:14:36 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
public static function isUserDefined($fq_class_name)
|
2016-10-25 17:40:09 +02:00
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
return self::$storage[strtolower($fq_class_name)]->user_defined;
|
2016-10-25 17:40:09 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 22:05:27 +02:00
|
|
|
/**
|
|
|
|
* Gets the method/function call map
|
|
|
|
*
|
2016-11-05 22:53:30 +01:00
|
|
|
* @return array<string, array<string, string>>
|
2016-11-05 23:46:17 +01:00
|
|
|
* @psalm-suppress MixedInferredReturnType as the use of require buggers things up
|
2017-01-09 05:58:06 +01:00
|
|
|
* @psalm-suppress MixedAssignment
|
2016-10-27 22:05:27 +02:00
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function getPropertyMap()
|
2016-10-27 22:05:27 +02:00
|
|
|
{
|
|
|
|
if (self::$property_map !== null) {
|
|
|
|
return self::$property_map;
|
|
|
|
}
|
|
|
|
|
2017-01-17 02:00:51 +01:00
|
|
|
/** @var array<string, array<string, string>> */
|
2016-10-27 22:05:27 +02:00
|
|
|
$property_map = require_once(__DIR__.'/../PropertyMap.php');
|
|
|
|
|
|
|
|
self::$property_map = [];
|
|
|
|
|
|
|
|
foreach ($property_map as $key => $value) {
|
|
|
|
$cased_key = strtolower($key);
|
|
|
|
self::$property_map[$cased_key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$property_map;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-27 22:05:27 +02:00
|
|
|
public static function inPropertyMap($class_name)
|
|
|
|
{
|
|
|
|
return isset(self::getPropertyMap()[strtolower($class_name)]);
|
|
|
|
}
|
|
|
|
|
2016-12-12 19:50:46 +01:00
|
|
|
/**
|
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param string $fq_trait_name
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function registerTraitUse($fq_class_name, $fq_trait_name)
|
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
$storage = self::$storage[strtolower($fq_class_name)];
|
2016-12-30 18:41:14 +01:00
|
|
|
|
|
|
|
$storage->used_traits[$fq_trait_name] = true;
|
2016-12-12 19:50:46 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-07-25 00:02:03 +02:00
|
|
|
public static function clearCache()
|
|
|
|
{
|
2016-12-30 18:41:14 +01:00
|
|
|
self::$file_classes = [];
|
2016-10-21 00:12:13 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
self::$trait_checkers = [];
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$class_checkers = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
self::$storage = [];
|
2016-12-12 19:50:46 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
self::$class_extends = [];
|
|
|
|
|
2016-08-15 17:01:50 +02:00
|
|
|
ClassChecker::clearCache();
|
2016-07-25 00:02:03 +02:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|