Test Failed
Pull Request — master (#5)
by Alex
07:05
created

AbstractFilter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 88
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validateFieldName() 0 10 3
A formatValue() 0 3 1
A resolveValue() 0 12 2
A resolveFieldName() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Query\Filter;
6
7
use Arp\LaminasDoctrine\Query\Exception\InvalidArgumentException;
8
use Arp\LaminasDoctrine\Query\Metadata\MetadataInterface;
9
use Arp\LaminasDoctrine\Query\QueryFilterManager;
10
11
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\LaminasDoctrine\Query\Filter
14
 */
15
abstract class AbstractFilter implements FilterInterface
16
{
17
    /**
18
     * @var QueryFilterManager
19
     */
20
    protected QueryFilterManager $queryFilterManager;
21
22
    /**
23
     * @param QueryFilterManager $queryFilterManager
24
     */
25
    public function __construct(QueryFilterManager $queryFilterManager)
26
    {
27
        $this->queryFilterManager = $queryFilterManager;
28
    }
29
30
    /**
31
     * @param MetadataInterface $metadata
32
     * @param array             $criteria
33
     * @param string            $key
34
     *
35
     * @return string
36
     */
37
    protected function resolveFieldName(MetadataInterface $metadata, array $criteria, string $key): string
0 ignored issues
show
Unused Code introduced by
The parameter $metadata is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

37
    protected function resolveFieldName(/** @scrutinizer ignore-unused */ MetadataInterface $metadata, array $criteria, string $key): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        if (empty($criteria[$key])) {
40
            throw new InvalidArgumentException(
41
                sprintf('The required \'%s\' is missing', $key)
42
            );
43
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 39 is false. This is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
44
    }
45
46
    /**
47
     * @param MetadataInterface $metadata
48
     * @param string            $fieldName
49
     * @param string            $entityName
50
     *
51
     * @throws InvalidArgumentException
52
     */
53
    protected function validateFieldName(MetadataInterface $metadata, string $fieldName, string $entityName): void
54
    {
55
        if (!$metadata->hasField($fieldName) && !$metadata->hasAssociation($fieldName)) {
56
            throw new InvalidArgumentException(
57
                sprintf(
58
                    'Unable to apply query filter \'%s\': '
59
                    . 'The entity class \'%s\' has no field or association named \'%s\'',
60
                    static::class,
61
                    $entityName,
62
                    $fieldName
63
                )
64
            );
65
        }
66
    }
67
68
    /**
69
     * @param string $fieldName
70
     * @param array  $criteria
71
     *
72
     * @return mixed
73
     *
74
     * @throws InvalidArgumentException
75
     */
76
    protected function resolveValue(string $fieldName, array $criteria)
77
    {
78
        if (!array_key_exists('value', $criteria)) {
79
            throw new InvalidArgumentException(
80
                sprintf(
81
                    'The required \'value\' criteria value is missing for filter \'%s::%s\'',
82
                    static::class,
83
                    $fieldName
84
                )
85
            );
86
        }
87
        return $criteria['value'];
88
    }
89
90
    /**
91
     * @param MetadataInterface $metadata
92
     * @param string            $fieldName
93
     * @param mixed             $value
94
     * @param string|null       $format
95
     *
96
     * @return mixed
97
     *
98
     * @noinspection PhpUnusedParameterInspection
99
     */
100
    protected function formatValue(MetadataInterface $metadata, string $fieldName, $value, ?string $format = null)
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

100
    protected function formatValue(MetadataInterface $metadata, string $fieldName, $value, /** @scrutinizer ignore-unused */ ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fieldName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

100
    protected function formatValue(MetadataInterface $metadata, /** @scrutinizer ignore-unused */ string $fieldName, $value, ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $metadata is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

100
    protected function formatValue(/** @scrutinizer ignore-unused */ MetadataInterface $metadata, string $fieldName, $value, ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
    {
102
        return $value;
103
    }
104
}
105