Passed
Branch master (7e4b7e)
by Mariano
04:51
created

ValidatorAnnotationParser   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 1
c 3
b 1
f 1
lcom 0
cbo 3
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 9 1
1
<?php
2
namespace Mcustiel\SimpleRequest\Strategies\Annotations;
3
4
use Mcustiel\SimpleRequest\Util\ValidatorBuilder;
5
use Mcustiel\SimpleRequest\Annotation\AnnotationWithAssociatedClass;
6
use Mcustiel\SimpleRequest\Annotation\RequestAnnotation;
7
use Mcustiel\SimpleRequest\Strategies\PropertyParserBuilder;
8
9
class ValidatorAnnotationParser implements AnnotationParser
10
{
11
    /**
12
     * {@inheritdoc}
13
     * In this method, annotation param is treated as instance of AnnotationWithAssociatedClass.
14
     *
15
     * @see \Mcustiel\SimpleRequest\Strategies\Annotations\AnnotationParser::execute()
16
     */
17 91
    public function execute(RequestAnnotation $annotation, PropertyParserBuilder $propertyParser)
18
    {
19 91
        $propertyParser->addValidator(
20 91
            ValidatorBuilder::builder()
0 ignored issues
show
Bug introduced by
It seems like \Mcustiel\SimpleRequest\...n->getValue())->build() targeting Mcustiel\SimpleRequest\U...ntationBuilder::build() can also be of type object<Mcustiel\SimpleRe...rfaces\FilterInterface>; however, Mcustiel\SimpleRequest\S...Builder::addValidator() does only seem to accept object<Mcustiel\SimpleRe...ces\ValidatorInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
21 91
            ->withClass($annotation->getAssociatedClass())
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Mcustiel\SimpleRequest\A...ation\RequestAnnotation as the method getAssociatedClass() does only exist in the following sub-classes of Mcustiel\SimpleRequest\A...ation\RequestAnnotation: Mcustiel\SimpleRequest\A...tionWithAssociatedClass, Mcustiel\SimpleRequest\Annotation\FilterAnnotation, Mcustiel\SimpleRequest\A...ation\Filter\Capitalize, Mcustiel\SimpleRequest\A...ion\Filter\CustomFilter, Mcustiel\SimpleRequest\A...ion\Filter\DefaultValue, Mcustiel\SimpleRequest\Annotation\Filter\LowerCase, Mcustiel\SimpleRequest\A...ion\Filter\RegexReplace, Mcustiel\SimpleRequest\A...ReplaceFilterAnnotation, Mcustiel\SimpleRequest\A...on\Filter\StringReplace, Mcustiel\SimpleRequest\Annotation\Filter\ToFloat, Mcustiel\SimpleRequest\Annotation\Filter\ToInteger, Mcustiel\SimpleRequest\Annotation\Filter\Trim, Mcustiel\SimpleRequest\Annotation\Filter\UpperCase, Mcustiel\SimpleRequest\A...ion\ValidatorAnnotation, Mcustiel\SimpleRequest\Annotation\Validator\AllOf, Mcustiel\SimpleRequest\Annotation\Validator\Alpha, Mcustiel\SimpleRequest\A...\Validator\AlphaNumeric, Mcustiel\SimpleRequest\Annotation\Validator\AnyOf, Mcustiel\SimpleRequest\A...lidator\CustomValidator, Mcustiel\SimpleRequest\A...tion\Validator\DateTime, Mcustiel\SimpleRequest\A...alidator\DateTimeFormat, Mcustiel\SimpleRequest\A...on\Validator\Definition, Mcustiel\SimpleRequest\Annotation\Validator\Email, Mcustiel\SimpleRequest\Annotation\Validator\Enum, Mcustiel\SimpleRequest\A...idator\ExclusiveMaximum, Mcustiel\SimpleRequest\A...idator\ExclusiveMinimum, Mcustiel\SimpleRequest\Annotation\Validator\Hexa, Mcustiel\SimpleRequest\A...tion\Validator\HostName, Mcustiel\SimpleRequest\Annotation\Validator\IPV4, Mcustiel\SimpleRequest\Annotation\Validator\IPV6, Mcustiel\SimpleRequest\Annotation\Validator\Items, Mcustiel\SimpleRequest\A...on\Validator\MacAddress, Mcustiel\SimpleRequest\A...tion\Validator\MaxItems, Mcustiel\SimpleRequest\A...ion\Validator\MaxLength, Mcustiel\SimpleRequest\A...Validator\MaxProperties, Mcustiel\SimpleRequest\A...ation\Validator\Maximum, Mcustiel\SimpleRequest\A...tion\Validator\MinItems, Mcustiel\SimpleRequest\A...ion\Validator\MinLength, Mcustiel\SimpleRequest\A...Validator\MinProperties, Mcustiel\SimpleRequest\A...ation\Validator\Minimum, Mcustiel\SimpleRequest\A...on\Validator\MultipleOf, Mcustiel\SimpleRequest\Annotation\Validator\Not, Mcustiel\SimpleRequest\A...tion\Validator\NotEmpty, Mcustiel\SimpleRequest\A...ation\Validator\NotNull, Mcustiel\SimpleRequest\Annotation\Validator\OneOf, Mcustiel\SimpleRequest\A...ation\Validator\Pattern, Mcustiel\SimpleRequest\A...on\Validator\Properties, Mcustiel\SimpleRequest\Annotation\Validator\RegExp, Mcustiel\SimpleRequest\A...tion\Validator\Required, Mcustiel\SimpleRequest\A...alidator\TwitterAccount, Mcustiel\SimpleRequest\Annotation\Validator\Type, Mcustiel\SimpleRequest\A...ion\Validator\TypeFloat, Mcustiel\SimpleRequest\A...n\Validator\TypeInteger, Mcustiel\SimpleRequest\A...n\Validator\UniqueItems, Mcustiel\SimpleRequest\Annotation\Validator\Uri. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(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 sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \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.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
22 91
            ->withSpecification($annotation->getValue())
23 91
            ->build()
24 91
        );
25 91
    }
26
}
27