Completed
Push — master ( a45e2b...7fff40 )
by Guillaume
03:21 queued 52s
created

mustBeInArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver\Validator;
5
6
use Imedia\Ammit\UI\Resolver\Exception\CommandMappingException;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
/**
10
 * @internal Contains Domain Validation assertions (but class won't be removed in next version)
11
 *   Domain Validation should be done in Domain
12
 *   Should be used for prototyping project knowing you are accumulating technical debt
13
 *
14
 * @author Guillaume MOREL <[email protected]>
15
 */
16 View Code Duplication
class PragmaticRequestQueryStringValueValidator extends RequestQueryStringValueValidator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /** @var RawValueValidator */
19
    protected $rawValueValidator;
20
21
    public function __construct(PragmaticRawValueValidator $rawValueValidator)
22
    {
23
        $this->rawValueValidator = $rawValueValidator;
24
    }
25
26
    /**
27
     * Domain should be responsible for id format
28
     * Exceptions are caught in order to be processed later
29
     *
30
     * @throws CommandMappingException If any mapping validation failed
31
     */
32
    public function mustBeUuid(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string
33
    {
34
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
35
36
        return $this->rawValueValidator->mustBeUuid(
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Imedia\Ammit\UI\Resolver...dator\RawValueValidator as the method mustBeUuid() does only exist in the following sub-classes of Imedia\Ammit\UI\Resolver...dator\RawValueValidator: Imedia\Ammit\UI\Resolver...gmaticRawValueValidator. 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...
37
            $value,
38
            $queryStringKey,
39
            $this,
40
            $exceptionMessage
41
        );
42
    }
43
44
    /**
45
     * Domain should be responsible for legit values
46
     * Exceptions are caught in order to be processed later
47
     *
48
     * @throws CommandMappingException If any mapping validation failed
49
     * @return mixed Untouched value
50
     */
51
    public function mustBeInArray(ServerRequestInterface $request, array $availableValues, string $queryStringKey, string $exceptionMessage = null)
52
    {
53
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
54
55
        return $this->rawValueValidator->mustBeInArray(
0 ignored issues
show
Bug introduced by
The method mustBeInArray() does not exist on Imedia\Ammit\UI\Resolver...dator\RawValueValidator. Did you maybe mean mustBeArray()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
56
            $value,
57
            $availableValues,
58
            $queryStringKey,
59
            $this,
60
            $exceptionMessage
61
        );
62
    }
63
64
    /**
65
     * Domain should be responsible for string emptiness
66
     * Exceptions are caught in order to be processed later
67
     *
68
     * @throws CommandMappingException If any mapping validation failed
69
     * @return mixed Untouched value
70
     */
71
    public function mustBeStringNotEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
72
    {
73
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
74
75
        return $this->rawValueValidator->mustBeStringNotEmpty(
0 ignored issues
show
Bug introduced by
The method mustBeStringNotEmpty() does not exist on Imedia\Ammit\UI\Resolver...dator\RawValueValidator. Did you maybe mean mustBeString()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
76
            $value,
77
            $queryStringKey,
78
            $this,
79
            $exceptionMessage
80
        );
81
    }
82
83
    /**
84
     * Domain should be responsible for id format
85
     * Exceptions are caught in order to be processed later
86
     *
87
     * @throws CommandMappingException If any mapping validation failed
88
     * @return mixed Untouched value
89
     */
90
    public function mustHaveLengthBetween(ServerRequestInterface $request, string $queryStringKey, int $min, int $max, string $exceptionMessage = null)
91
    {
92
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
93
94
        return $this->rawValueValidator->mustHaveLengthBetween(
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Imedia\Ammit\UI\Resolver...dator\RawValueValidator as the method mustHaveLengthBetween() does only exist in the following sub-classes of Imedia\Ammit\UI\Resolver...dator\RawValueValidator: Imedia\Ammit\UI\Resolver...gmaticRawValueValidator. 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...
95
            $value,
96
            $queryStringKey,
97
            $min,
98
            $max,
99
            $this,
100
            $exceptionMessage
101
        );
102
    }
103
104
    /**
105
     * Domain should be responsible for email format
106
     * Exceptions are caught in order to be processed later
107
     *
108
     * @throws CommandMappingException If any mapping validation failed
109
     * @return mixed Untouched value
110
     */
111
    public function mustBeEmailAddress(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
112
    {
113
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
114
115
        return $this->rawValueValidator->mustBeEmailAddress(
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Imedia\Ammit\UI\Resolver...dator\RawValueValidator as the method mustBeEmailAddress() does only exist in the following sub-classes of Imedia\Ammit\UI\Resolver...dator\RawValueValidator: Imedia\Ammit\UI\Resolver...gmaticRawValueValidator. 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...
116
            $value,
117
            $queryStringKey,
118
            $this,
119
            $exceptionMessage
120
        );
121
    }
122
123
    /**
124
     * Domain should be responsible for regex validation
125
     * Exceptions are caught in order to be processed later
126
     *
127
     * @throws CommandMappingException If any mapping validation failed
128
     * @return mixed Untouched value
129
     */
130
    public function mustBeValidAgainstRegex(ServerRequestInterface $request, string $pattern, string $queryStringKey, string $exceptionMessage = null)
131
    {
132
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
133
134
        return $this->rawValueValidator->mustBeValidAgainstRegex(
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Imedia\Ammit\UI\Resolver...dator\RawValueValidator as the method mustBeValidAgainstRegex() does only exist in the following sub-classes of Imedia\Ammit\UI\Resolver...dator\RawValueValidator: Imedia\Ammit\UI\Resolver...gmaticRawValueValidator. 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...
135
            $value,
136
            $pattern,
137
            $queryStringKey,
138
            $this,
139
            $exceptionMessage
140
        );
141
    }
142
}
143