public getMethodByFQSEN ( |
||
$fqsen | The FQSEN of a method to get | |
return | A method with the given FQSEN |
/** * Check to see if the given Clazz is a duplicate * * @return null */ public static function analyzeDuplicateFunction(CodeBase $code_base, FunctionInterface $method) { $fqsen = $method->getFQSEN(); if (!$fqsen->isAlternate()) { return; } $original_fqsen = $fqsen->getCanonicalFQSEN(); if ($original_fqsen instanceof FullyQualifiedFunctionName) { if (!$code_base->hasFunctionWithFQSEN($original_fqsen)) { return; } $original_method = $code_base->getFunctionByFQSEN($original_fqsen); } else { if (!$code_base->hasMethodWithFQSEN($original_fqsen)) { return; } $original_method = $code_base->getMethodByFQSEN($original_fqsen); } $method_name = $method->getName(); if (!$method->hasSuppressIssue(Issue::RedefineFunction)) { if ($original_method->isInternal()) { Issue::maybeEmit($code_base, $method->getContext(), Issue::RedefineFunctionInternal, $method->getFileRef()->getLineNumberStart(), $method_name, $method->getFileRef()->getFile(), $method->getFileRef()->getLineNumberStart()); } else { Issue::maybeEmit($code_base, $method->getContext(), Issue::RedefineFunction, $method->getFileRef()->getLineNumberStart(), $method_name, $method->getFileRef()->getFile(), $method->getFileRef()->getLineNumberStart(), $original_method->getFileRef()->getFile(), $original_method->getFileRef()->getLineNumberStart()); } } }
/** * @return Method[]|\Generator * The set of all alternates to this method */ public function alternateGenerator(CodeBase $code_base) : \Generator { $alternate_id = 0; $fqsen = $this->getFQSEN(); while ($code_base->hasMethodWithFQSEN($fqsen)) { (yield $code_base->getMethodByFQSEN($fqsen)); $fqsen = $fqsen->withAlternateId(++$alternate_id); } }
/** * @param CodeBase $code_base * The global code base holding all state * * @return FunctionInterface * Get the method in this scope or fail real hard */ public function getMethodInScope(CodeBase $code_base) : FunctionInterface { assert($this->isMethodScope(), "Must be in method scope to get method. Actually in {$this}"); return $this->getMethodFQSEN() instanceof FullyQualifiedFunctionName ? $code_base->getFunctionByFQSEN($this->getMethodFQSEN()) : $code_base->getMethodByFQSEN($this->getMethodFQSEN()); }
/** * Add a method to this class * * @param CodeBase $code_base * A reference to the code base in which the ancestor exists * * @param Method $method * The method to copy onto this class * * @param Option<Type>|None $type_option * A possibly defined type used to define template * parameter types when importing the method * * @return null */ public function addMethod(CodeBase $code_base, Method $method, $type_option) { $method_fqsen = FullyQualifiedMethodName::make($this->getFQSEN(), $method->getName(), $method->getFQSEN()->getAlternateId()); // Don't overwrite overridden methods with // parent methods if ($code_base->hasMethodWithFQSEN($method_fqsen)) { // Note that we're overriding something $existing_method = $code_base->getMethodByFQSEN($method_fqsen); $existing_method->setIsOverride(true); // Don't add the method return; } if ($method->getFQSEN() !== $method_fqsen) { $method = clone $method; $method->setDefiningFQSEN($method->getFQSEN()); $method->setFQSEN($method_fqsen); // If we have a parent type defined, map the method's // return type and parameter types through it if ($type_option->isDefined()) { // Map the method's return type if ($method->getUnionType()->hasTemplateType()) { $method->setUnionType($method->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base))); } // Map each method parameter $method->setParameterList(array_map(function (Parameter $parameter) use($type_option, $code_base) : Parameter { if (!$parameter->getUnionType()->hasTemplateType()) { return $parameter; } $mapped_parameter = clone $parameter; $mapped_parameter->setUnionType($mapped_parameter->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base))); return $mapped_parameter; }, $method->getParameterList())); } } if ($method->getHasYield()) { // There's no phpdoc standard for template types of Generators at the moment. $newType = UnionType::fromFullyQualifiedString('\\Generator'); $oldType = $method->getUnionType(); if (!$newType->canCastToUnionType($method->getUnionType())) { $method->setUnionType($newType); } } $code_base->addMethod($method); }
/** * @return null */ public function addMethod(CodeBase $code_base, Method $method) { $method_fqsen = FullyQualifiedMethodName::make($this->getFQSEN(), $method->getName()); // Don't overwrite overridden methods with // parent methods if ($code_base->hasMethodWithFQSEN($method_fqsen)) { // Note that we're overriding something $existing_method = $code_base->getMethodByFQSEN($method_fqsen); $existing_method->setIsOverride(true); // Don't add the method return; } if ($method->getFQSEN() !== $method_fqsen) { $method = clone $method; $method->setFQSEN($method_fqsen); } $code_base->addMethod($method); }
/** * @param CodeBase $code_base * The global code base holding all state * * @return Element\Func|Element\Method * Get the method in this scope or fail real hard */ public function getFunctionLikeInScope(CodeBase $code_base) : FunctionInterface { assert($this->isInFunctionLikeScope(), "Must be in method scope to get method."); $fqsen = $this->getFunctionLikeFQSEN(); if ($fqsen instanceof FullyQualifiedFunctionName) { assert($code_base->hasFunctionWithFQSEN($fqsen), "The function does not exist"); return $code_base->getFunctionByFQSEN($fqsen); } if ($fqsen instanceof FullyQualifiedMethodName) { assert($code_base->hasMethodWithFQSEN($fqsen), "Method does not exist"); return $code_base->getMethodByFQSEN($fqsen); } assert(false, "FQSEN must be for a function or method"); }
/** * Add a method to this class * * @param CodeBase $code_base * A reference to the code base in which the ancestor exists * * @param Method $method * The method to copy onto this class * * @param Option<Type> $type_option * A possibly defined type used to define template * parameter types when importing the method * * @return null */ public function addMethod(CodeBase $code_base, Method $method, $type_option) { $method_fqsen = FullyQualifiedMethodName::make($this->getFQSEN(), $method->getName()); // Don't overwrite overridden methods with // parent methods if ($code_base->hasMethodWithFQSEN($method_fqsen)) { // Note that we're overriding something $existing_method = $code_base->getMethodByFQSEN($method_fqsen); $existing_method->setIsOverride(true); // Don't add the method return; } if ($method->getFQSEN() !== $method_fqsen) { $method = clone $method; $method->setDefiningFQSEN($method->getFQSEN()); $method->setFQSEN($method_fqsen); // If we have a parent type defined, map the method's // return type and parameter types through it if ($type_option->isDefined()) { // Map the method's return type if ($method->getUnionType()->hasTemplateType()) { $method->setUnionType($method->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base))); } // Map each method parameter $method->setParameterList(array_map(function (Parameter $parameter) use($type_option, $code_base) : Parameter { if (!$parameter->getUnionType()->hasTemplateType()) { return $parameter; } $mapped_parameter = clone $parameter; $mapped_parameter->setUnionType($mapped_parameter->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base))); return $mapped_parameter; }, $method->getParameterList())); } } $code_base->addMethod($method); }