Filter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 86.27%

Importance

Changes 0
Metric Value
wmc 10
eloc 58
c 0
b 0
f 0
dl 0
loc 122
rs 10
ccs 44
cts 51
cp 0.8627

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValuesText() 0 19 3
A init() 0 66 4
A disableExtraFields() 0 4 1
A getValues() 0 9 2
1
<?php
2
3
namespace mQueue\Form;
4
5
use mQueue\Model\Status;
6
use mQueue\Model\UserMapper;
7
use Zend_Form_SubForm;
8
9
class Filter extends Zend_Form_SubForm
10
{
11 1
    public function init(): void
12
    {
13
        // Set the method for the display form to GET
14 1
        $this->setMethod('get');
15
16 1
        $users = [];
17 1
        if (\mQueue\Model\User::getCurrent()) {
18
            $users = [0 => _tr('<< me >>')];
19
        }
20
21 1
        foreach (UserMapper::fetchAll() as $user) {
22 1
            $users[$user->id] = $user->nickname;
0 ignored issues
show
Bug Best Practice introduced by
The property nickname does not exist on mQueue\Model\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property id does not exist on mQueue\Model\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
23
        }
24
25 1
        $this->addElement('select', 'user', [
26 1
            'multiOptions' => $users,
27
            'required' => true,
28 1
            'class' => 'filterUser',
29
            'validators' => [
30 1
                ['validator' => new Validate\User()],
31
            ],
32
            'filters' => [
33
                ['int'],
34
            ],
35
        ]);
36
37 1
        $this->addElement('select', 'condition', [
38 1
            'multiOptions' => ['is' => _tr('rating is'), 'isnot' => _tr('rating is not')],
39
            'required' => true,
40
        ]);
41
42 1
        $statuses = [];
43 1
        foreach (Status::$ratings as $rating => $label) {
44 1
            $statuses[$rating] = $this->getView()->rating($rating);
45
        }
46 1
        $statuses = $statuses + [0 => _tr('nothing')];
47
48 1
        $this->addElement('multiCheckbox', 'status', [
49 1
            'escape' => false,
50 1
            'multiOptions' => $statuses,
51
            'required' => true,
52 1
            'separator' => ' ',
53
            'filters' => [
54
                ['int'],
55
            ],
56
        ]);
57 1
        $this->status->getDecorator('HtmlTag')->setOption('class', 'filterStatus');
0 ignored issues
show
Bug Best Practice introduced by
The property status does not exist on mQueue\Form\Filter. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getDecorator() does not exist on null. ( Ignorable by Annotation )

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

57
        $this->status->/** @scrutinizer ignore-call */ 
58
                       getDecorator('HtmlTag')->setOption('class', 'filterStatus');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
59
        // Add the title element
60 1
        $this->addElement('text', 'title', [
61 1
            'placeholder' => _tr('title'),
62
            'autofocus' => true,
63
            'filters' => [
64
                ['stringTrim'],
65
            ],
66
        ]);
67
68
        // Add the filter element
69 1
        $this->addElement('checkbox', 'withSource', [
70 1
            'label' => _tr('With source'),
71
        ]);
72 1
        $this->withSource->getDecorator('Label')->setOptions(['placement' => 'append']);
0 ignored issues
show
Bug Best Practice introduced by
The property withSource does not exist on mQueue\Form\Filter. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getDecorator() does not exist on null. ( Ignorable by Annotation )

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

72
        $this->withSource->/** @scrutinizer ignore-call */ 
73
                           getDecorator('Label')->setOptions(['placement' => 'append']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
74 1
        $this->setDecorators([
75 1
            'FormElements',
76
            [['row' => 'HtmlTag'], ['tag' => 'dl', 'class' => 'filter']],
77
        ]);
78 1
    }
79
80
    /**
81
     * Disable extra field elements
82
     */
83
    public function disableExtraFields(): void
84
    {
85
        $this->removeElement('title');
86
        $this->removeElement('withSource');
87
    }
88
89
    /**
90
     * Override getValues() to replace special '0' value with current user
91
     *
92
     * @param bool $suppressArrayNotation
93
     *
94
     * @return array values
95
     */
96 1
    public function getValues($suppressArrayNotation = false)
97
    {
98 1
        $values = parent::getValues($suppressArrayNotation);
99
100 1
        if ($values['user'] == '0') {
101
            $values['user'] = \mQueue\Model\User::getCurrent()->id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on mQueue\Model\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
102
        }
103
104 1
        return $values;
105
    }
106
107
    /**
108
     * Returns values as readable text for end-user
109
     *
110
     * @return string
111
     */
112 1
    public function getValuesText()
113
    {
114 1
        $text = '';
115 1
        $values = $this->getValues(true);
116
117 1
        if (@$values['title']) {
118
            $text = _tr('title') . ':"' . $values['title'] . '" + ';
119
        }
120
121 1
        $users = $this->getElement('user')->getMultiOptions();
122 1
        $statuses = $this->getElement('status')->getMultiOptions();
123
124 1
        $statusLabels = [];
125 1
        foreach ($values['status'] as $status) {
126 1
            $statusLabels[] = Status::$ratings[$status] ?? $statuses[$status];
127
        }
128 1
        $text .= $users[$values['user']] . ':' . implode('+', $statusLabels);
129
130 1
        return $text;
131
    }
132
}
133