Completed
Push — master ( abd78d...b723d6 )
by Guillaume
26:11 queued 17:30
created

mustBeStringNotEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
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
class PragmaticRequestQueryStringValueValidator extends RequestQueryStringValueValidator
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
            $min,
97
            $max,
98
            $queryStringKey,
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
    /**
144
     * Exceptions are caught in order to be processed later
145
     *
146
     * @throws CommandMappingException If any mapping validation failed
147
     * @return int|null
148
     */
149
    public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
150
    {
151
        $value = $this->extractValueFromRequestQueryString($request, $attributeKey);
152
153
        return $this->rawValueValidator->mustBeIntegerOrEmpty(
154
            $value,
155
            $attributeKey,
156
            $this,
157
            $exceptionMessage
158
        );
159
    }
160
}
161