The trait Idable provides a method equalsId that in turn relies on the
method getId(). If this method does not exist on a class mixing in this
trait, the method will fail.
Adding the getId() as an abstract method to the trait will make sure it
is available.
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\NodeInterface as the method toPrimitive() does only exist in the following implementations of said interface: Railt\SDL\Compiler\Ast\Value\BooleanValueNode, Railt\SDL\Compiler\Ast\Value\ConstantValueNode, Railt\SDL\Compiler\Ast\Value\InputValueNode, Railt\SDL\Compiler\Ast\Value\ListValueNode, Railt\SDL\Compiler\Ast\Value\NullValueNode, Railt\SDL\Compiler\Ast\Value\NumberValueNode, Railt\SDL\Compiler\Ast\Value\StringValueNode, Railt\SDL\Compiler\Ast\Value\ValueNode.
Let’s take a look at an example:
interfaceUser{/** @return string */publicfunctiongetPassword();}classMyUserimplementsUser{publicfunctiongetPassword(){// return something}publicfunctiongetDisplayName(){// return some name.}}classAuthSystem{publicfunctionauthenticate(User$user){$this->logger->info(sprintf('Authenticating %s.',$user->getDisplayName()));// do something.}}
In the above example, the authenticate() method works fine as long as you just pass
instances of MyUser. However, if you now also want to pass a different implementation
of User which does not have a getDisplayName() method, the code will break.
classAuthSystem{publicfunctionauthenticate(User$user){if($userinstanceofMyUser){$this->logger->info(/** ... */);}// or alternativelyif(!$userinstanceofMyUser){thrownew\LogicException('$user must be an instance of MyUser, '.'other instances are not supported.');}}}
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.