From 283e0a6af257b1f530eb3ece92d2a9cc63733338 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Thu, 8 Feb 2018 18:18:34 -0500 Subject: [PATCH] Migrate class constant handling into codebase classes --- src/Psalm/Checker/ClassLikeChecker.php | 35 ------------------- .../Expression/Fetch/ConstFetchChecker.php | 6 ++-- src/Psalm/Codebase/ClassLikes.php | 34 ++++++++++++++++++ 3 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/Psalm/Checker/ClassLikeChecker.php b/src/Psalm/Checker/ClassLikeChecker.php index 1efaf6f9d..8d61dd762 100644 --- a/src/Psalm/Checker/ClassLikeChecker.php +++ b/src/Psalm/Checker/ClassLikeChecker.php @@ -17,7 +17,6 @@ use Psalm\Provider\FileReferenceProvider; use Psalm\StatementsSource; use Psalm\Storage\ClassLikeStorage; use Psalm\Type; -use ReflectionProperty; abstract class ClassLikeChecker extends SourceChecker implements StatementsSource { @@ -430,40 +429,6 @@ abstract class ClassLikeChecker extends SourceChecker implements StatementsSourc } } - /** - * @param string $class_name - * @param mixed $visibility - * - * @return array - */ - public static function getConstantsForClass(ProjectChecker $project_checker, $class_name, $visibility) - { - $class_name = strtolower($class_name); - - $storage = $project_checker->classlike_storage_provider->get($class_name); - - if ($visibility === ReflectionProperty::IS_PUBLIC) { - return $storage->public_class_constants; - } - - if ($visibility === ReflectionProperty::IS_PROTECTED) { - return array_merge( - $storage->public_class_constants, - $storage->protected_class_constants - ); - } - - if ($visibility === ReflectionProperty::IS_PRIVATE) { - return array_merge( - $storage->public_class_constants, - $storage->protected_class_constants, - $storage->private_class_constants - ); - } - - throw new \InvalidArgumentException('Must specify $visibility'); - } - /** * @param string $property_id * @param string|null $calling_context diff --git a/src/Psalm/Checker/Statements/Expression/Fetch/ConstFetchChecker.php b/src/Psalm/Checker/Statements/Expression/Fetch/ConstFetchChecker.php index 0d0cf09c2..8085c6eb5 100644 --- a/src/Psalm/Checker/Statements/Expression/Fetch/ConstFetchChecker.php +++ b/src/Psalm/Checker/Statements/Expression/Fetch/ConstFetchChecker.php @@ -161,8 +161,7 @@ class ConstFetchChecker $class_visibility = \ReflectionProperty::IS_PUBLIC; } - $class_constants = ClassLikeChecker::getConstantsForClass( - $project_checker, + $class_constants = $codebase->classlikes->getConstantsForClass( $fq_class_name, $class_visibility ); @@ -171,8 +170,7 @@ class ConstFetchChecker $all_class_constants = []; if ($fq_class_name !== $context->self) { - $all_class_constants = ClassLikeChecker::getConstantsForClass( - $project_checker, + $all_class_constants = $codebase->classlikes->getConstantsForClass( $fq_class_name, \ReflectionProperty::IS_PRIVATE ); diff --git a/src/Psalm/Codebase/ClassLikes.php b/src/Psalm/Codebase/ClassLikes.php index 6eb632361..64ffb92fc 100644 --- a/src/Psalm/Codebase/ClassLikes.php +++ b/src/Psalm/Codebase/ClassLikes.php @@ -617,6 +617,40 @@ class ClassLikes } } + /** + * @param string $class_name + * @param mixed $visibility + * + * @return array + */ + public function getConstantsForClass($class_name, $visibility) + { + $class_name = strtolower($class_name); + + $storage = $this->classlike_storage_provider->get($class_name); + + if ($visibility === ReflectionProperty::IS_PUBLIC) { + return $storage->public_class_constants; + } + + if ($visibility === ReflectionProperty::IS_PROTECTED) { + return array_merge( + $storage->public_class_constants, + $storage->protected_class_constants + ); + } + + if ($visibility === ReflectionProperty::IS_PRIVATE) { + return array_merge( + $storage->public_class_constants, + $storage->protected_class_constants, + $storage->private_class_constants + ); + } + + throw new \InvalidArgumentException('Must specify $visibility'); + } + /** * @param string $class_name * @param string $const_name