getCodebase(); $config = $codebase->config; $late_static = false; if ($stmt->class instanceof PhpParser\Node\Name) { if (!in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)) { $aliases = $statements_analyzer->getAliases(); if ($context->calling_method_id && !$stmt->class instanceof PhpParser\Node\Name\FullyQualified ) { $codebase->file_reference_provider->addReferenceToClassMethod( $context->calling_method_id, 'use:' . $stmt->class->parts[0] . ':' . \md5($statements_analyzer->getFilePath()) ); } $fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject( $stmt->class, $aliases ); $fq_class_name = $codebase->classlikes->getUnAliasedName($fq_class_name); } else { switch ($stmt->class->parts[0]) { case 'self': $fq_class_name = $context->self; break; case 'parent': $fq_class_name = $context->parent; break; case 'static': // @todo maybe we can do better here $fq_class_name = $context->self; $late_static = true; break; } } if ($codebase->server_mode && $fq_class_name) { $codebase->analyzer->addNodeReference( $statements_analyzer->getFilePath(), $stmt->class, $fq_class_name ); } } elseif ($stmt->class instanceof PhpParser\Node\Stmt\Class_) { $statements_analyzer->analyze([$stmt->class], $context); $fq_class_name = ClassAnalyzer::getAnonymousClassName($stmt->class, $statements_analyzer->getFilePath()); } else { ExpressionAnalyzer::analyze($statements_analyzer, $stmt->class, $context); if (isset($stmt->class->inferredType)) { $has_single_class = $stmt->class->inferredType->isSingleStringLiteral(); if ($has_single_class) { $fq_class_name = $stmt->class->inferredType->getSingleStringLiteral()->value; } else { $generic_params = null; if (self::checkMethodArgs( null, $stmt->args, $generic_params, $context, new CodeLocation($statements_analyzer->getSource(), $stmt), $statements_analyzer ) === false) { return false; } } $new_type = null; foreach ($stmt->class->inferredType->getTypes() as $lhs_type_part) { if ($lhs_type_part instanceof Type\Atomic\TGenericParamClass) { if (!isset($stmt->inferredType)) { $new_type_part = new Type\Atomic\TGenericParam( $lhs_type_part->param_name, $lhs_type_part->extends ); if ($new_type) { $new_type = Type::combineUnionTypes( $new_type, new Type\Union([$new_type_part]) ); } else { $new_type = new Type\Union([$new_type_part]); } } continue; } if ($lhs_type_part instanceof Type\Atomic\TLiteralClassString || $lhs_type_part instanceof Type\Atomic\TClassString ) { if (!isset($stmt->inferredType)) { $class_name = $lhs_type_part instanceof Type\Atomic\TClassString ? $lhs_type_part->extends : $lhs_type_part->value; if ($class_name === 'object') { if (IssueBuffer::accepts( new MixedMethodCall( 'Cannot call constructor on an unknown class', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() )) { // fall through } } if ($new_type) { $new_type = Type::combineUnionTypes( $new_type, Type::parseString($class_name) ); } else { $new_type = Type::parseString($class_name); } } continue; } if ($lhs_type_part instanceof Type\Atomic\TString) { if ($config->allow_string_standin_for_class && !$lhs_type_part instanceof Type\Atomic\TNumericString ) { // do nothing } elseif (IssueBuffer::accepts( new InvalidStringClass( 'String cannot be used as a class', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() )) { // fall through } } elseif ($lhs_type_part instanceof Type\Atomic\TMixed || $lhs_type_part instanceof Type\Atomic\TGenericParam ) { if (IssueBuffer::accepts( new MixedMethodCall( 'Cannot call constructor on an unknown class', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() )) { // fall through } } elseif ($lhs_type_part instanceof Type\Atomic\TFalse && $stmt->class->inferredType->ignore_falsable_issues ) { // do nothing } elseif ($lhs_type_part instanceof Type\Atomic\TNull && $stmt->class->inferredType->ignore_nullable_issues ) { // do nothing } elseif (IssueBuffer::accepts( new UndefinedClass( 'Type ' . $lhs_type_part . ' cannot be called as a class', new CodeLocation($statements_analyzer->getSource(), $stmt), (string)$lhs_type_part ), $statements_analyzer->getSuppressedIssues() )) { // fall through } if ($new_type) { $new_type = Type::combineUnionTypes( $new_type, Type::getObject() ); } else { $new_type = Type::getObject(); } } if (!$has_single_class) { if ($new_type) { $stmt->inferredType = $new_type; } return null; } } else { return null; } } if ($fq_class_name) { if ($context->check_classes) { if ($context->isPhantomClass($fq_class_name)) { return null; } if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName( $statements_analyzer, $fq_class_name, new CodeLocation($statements_analyzer->getSource(), $stmt->class), $statements_analyzer->getSuppressedIssues(), false ) === false) { return false; } if ($codebase->interfaceExists($fq_class_name)) { if (IssueBuffer::accepts( new InterfaceInstantiation( 'Interface ' . $fq_class_name . ' cannot be instantiated', new CodeLocation($statements_analyzer->getSource(), $stmt->class) ), $statements_analyzer->getSuppressedIssues() )) { return false; } return null; } } $stmt->inferredType = new Type\Union([new TNamedObject($fq_class_name)]); if (strtolower($fq_class_name) !== 'stdclass' && $context->check_classes && $codebase->classlikes->classExists($fq_class_name) ) { $storage = $codebase->classlike_storage_provider->get($fq_class_name); // if we're not calling this constructor via new static() if ($storage->abstract && !$late_static) { if (IssueBuffer::accepts( new AbstractInstantiation( 'Unable to instantiate a abstract class ' . $fq_class_name, new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() )) { return false; } } if ($storage->deprecated) { if (IssueBuffer::accepts( new DeprecatedClass( $fq_class_name . ' is marked deprecated', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() )) { // fall through } } if ($storage->internal && $context->self) { $self_root = preg_replace('/^([^\\\]+).*/', '$1', $context->self); $declaring_root = preg_replace('/^([^\\\]+).*/', '$1', $fq_class_name); if (strtolower($self_root) !== strtolower($declaring_root)) { if (IssueBuffer::accepts( new InternalClass( $fq_class_name . ' is marked internal', new CodeLocation($statements_analyzer->getSource(), $stmt) ), $statements_analyzer->getSuppressedIssues() )) { // fall through } } } $has_generic_object = false; if ($codebase->methods->methodExists( $fq_class_name . '::__construct', $context->calling_method_id, $context->collect_references ? new CodeLocation($statements_analyzer->getSource(), $stmt) : null )) { $method_id = $fq_class_name . '::__construct'; if (self::checkMethodArgs( $method_id, $stmt->args, $found_generic_params, $context, new CodeLocation($statements_analyzer->getSource(), $stmt), $statements_analyzer ) === false) { return false; } if (MethodAnalyzer::checkMethodVisibility( $method_id, $context->self, $statements_analyzer->getSource(), new CodeLocation($statements_analyzer->getSource(), $stmt), $statements_analyzer->getSuppressedIssues() ) === false) { return false; } $generic_params = null; if ($storage->template_types) { foreach ($storage->template_types as $template_name => $_) { if (isset($found_generic_params[$template_name])) { $generic_params[] = $found_generic_params[$template_name]; } else { $generic_params[] = Type::getMixed(); } } } if ($fq_class_name === 'ArrayIterator' && isset($stmt->args[0]->value->inferredType)) { $first_arg_type = $stmt->args[0]->value->inferredType; if ($first_arg_type->hasGeneric()) { $key_type = null; $value_type = null; foreach ($first_arg_type->getTypes() as $type) { if ($type instanceof Type\Atomic\TArray) { $first_type_param = count($type->type_params) ? $type->type_params[0] : null; $last_type_param = $type->type_params[count($type->type_params) - 1]; if ($value_type === null) { $value_type = clone $last_type_param; } else { $value_type = Type::combineUnionTypes($value_type, $last_type_param); } if (!$key_type || !$first_type_param) { $key_type = $first_type_param ? clone $first_type_param : Type::getMixed(); } else { $key_type = Type::combineUnionTypes($key_type, $first_type_param); } } } if ($key_type === null) { throw new \UnexpectedValueException('$key_type cannot be null'); } if ($value_type === null) { throw new \UnexpectedValueException('$value_type cannot be null'); } $has_generic_object = true; $stmt->inferredType = new Type\Union([ new Type\Atomic\TGenericObject( $fq_class_name, [ $key_type, $value_type, ] ), ]); } } elseif ($generic_params) { $has_generic_object = true; $stmt->inferredType = new Type\Union([ new Type\Atomic\TGenericObject( $fq_class_name, $generic_params ), ]); } } elseif ($stmt->args) { if (IssueBuffer::accepts( new TooManyArguments( 'Class ' . $fq_class_name . ' has no __construct, but arguments were passed', new CodeLocation($statements_analyzer->getSource(), $stmt), $fq_class_name . '::__construct' ), $statements_analyzer->getSuppressedIssues() )) { // fall through } } if (!$has_generic_object && $codebase->classlikes->classImplements( $fq_class_name, 'IteratorAggregate' ) && $codebase->methods->methodExists( $fq_class_name . '::getIterator', $context->calling_method_id ) ) { $return_type_candidate = $codebase->methods->getMethodReturnType( $fq_class_name . '::getIterator', $self_fq_class_name ); if ($return_type_candidate && $return_type_candidate->isSingle()) { $return_type_atomic_candidate = array_values($return_type_candidate->getTypes())[0]; if ($return_type_atomic_candidate instanceof Type\Atomic\TGenericObject) { $stmt->inferredType = new Type\Union([ new Type\Atomic\TGenericObject( $fq_class_name, $return_type_atomic_candidate->type_params ), ]); } } } } } if (!$config->remember_property_assignments_after_call && !$context->collect_initializations) { $context->removeAllObjectVars(); } return null; } }