PragmaticRequestQueryStringValueValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 145
Duplicated Lines 48.28 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 70
loc 145
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A mustBeStringNotEmpty() 11 11 1
A mustHaveLengthBetween() 13 13 1
A mustBeUuid() 11 11 1
A mustBeInArray() 12 12 1
A mustBeEmailAddress() 11 11 1
A mustBeValidAgainstRegex() 12 12 1
A __construct() 0 4 1
A mustBeIntegerOrEmpty() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public function mustBeUuid(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    public function mustBeInArray(ServerRequestInterface $request, array $availableValues, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    public function mustBeStringNotEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    public function mustHaveLengthBetween(ServerRequestInterface $request, string $queryStringKey, int $min, int $max, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    public function mustBeEmailAddress(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    public function mustBeValidAgainstRegex(ServerRequestInterface $request, string $pattern, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method 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...
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