Completed
Push — develop ( d6e286...1202a6 )
by
unknown
08:31
created

BaseFieldset::getLocationsSummaryValue()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 15
nc 6
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author    [email protected]
9
 */
10
11
namespace Jobs\Form;
12
13
use Core\Entity\EntityInterface;
14
use Core\Entity\EntityTrait;
15
use Core\Entity\Hydrator\MappingEntityHydrator;
16
use Core\Form\HydratorStrategyAwareTrait;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Jobs\Entity\Location;
19
use Jobs\Form\Hydrator\Strategy\LocationStrategy;
20
use Zend\Form\Exception;
21
use Zend\Form\Fieldset;
22
use Core\Entity\Hydrator\EntityHydrator;
23
use Zend\Form\FieldsetInterface;
24
use Core\Form\CustomizableFieldsetInterface;
25
use Core\Form\CustomizableFieldsetTrait;
26
27
/**
28
 * Defines the formular fields of the base formular of a job opening.
29
 */
30
class BaseFieldset extends Fieldset implements CustomizableFieldsetInterface
31
{
32
    use CustomizableFieldsetTrait;
33
34
    /**
35
     * @return \Zend\Hydrator\HydratorInterface
36
     */
37
    public function getHydrator()
38
    {
39
        if (!$this->hydrator) {
40
            $hydrator = new MappingEntityHydrator([
41
                'locations' => 'geoLocation'
42
            ]);
43
44
            $geoLocationIsMultiple = $this->get('geoLocation')->getAttribute('multiple', false);
0 ignored issues
show
Unused Code introduced by
The call to ElementInterface::getAttribute() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
            $geoLocationStrategy = $this->get('geoLocation')->getHydratorStrategy();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Form\ElementInterface as the method getHydratorStrategy() does only exist in the following implementations of said interface: Core\Form\Tree\Select, Geo\Form\GeoSelect, Geo\Form\GeoSelectSimple.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
46
47
            $locationsStrategy = new \Zend\Hydrator\Strategy\ClosureStrategy(
48
                /* extract */
49
                function ($value) use ($geoLocationStrategy, $geoLocationIsMultiple)
50
                {
51
                    $value = $geoLocationIsMultiple ? $value : $value->first();
52
53
                    return $geoLocationStrategy->extract($value);
54
                },
55
56
                /* hydrate */
57
                function ($value) use ($geoLocationStrategy, $geoLocationIsMultiple)
58
                {
59
                    if ($geoLocationIsMultiple) {
60
                        return $geoLocationStrategy->hydrate($value);
61
                    }
62
63
                    return new ArrayCollection([$geoLocationStrategy->hydrate($value)]);
64
                }
65
            );
66
67
            $hydrator->addStrategy('locations', $locationsStrategy);
68
69
            /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
            $datetimeStrategy = new Hydrator\DatetimeStrategy();
71
            $datetimeStrategy->setHydrateFormat(Hydrator\DatetimeStrategy::FORMAT_MYSQLDATE);
72
            $hydrator->addStrategy('datePublishStart', $datetimeStrategy);
73
            */
74
75
            $this->setHydrator($hydrator);
76
        }
77
        return $this->hydrator;
78
    }
79
80
    public function init()
81
    {
82
        $this->setAttribute('id', 'job-fieldset');
83
84
        $this->setName('jobBase');
85
86
        $this->add(
87
            [
88
                'type' => 'Text',
89
                'name' => 'title',
90
                'options' => [
91
                    'label' => /*@translate*/ 'Job title',
92
                    'description' => /*@translate*/ 'Please enter the job title'
93
                ],
94
            ]
95
        );
96
97
98
        $this->add(
99
            [
100
                'type' => 'LocationSelect',
101
                'name' => 'geoLocation',
102
                'options' => [
103
                    'label' => /*@translate*/ 'Location',
104
                    'description' => /*@translate*/ 'Please enter the location of the job',
105
                    'location_entity' => Location::class,
106
                    'summary_value' => [$this, 'getLocationsSummaryValue'],
107
                ],
108
                'attributes' => [
109
                    'data-width' => '100%',
110
                ]
111
            ]
112
        );
113
114
    }
115
116
    /**
117
     *
118
     * @codeCoverageIgnore
119
     * @return string
120
     */
121
    public function getLocationsSummaryValue()
122
    {
123
        $element = $this->get('geoLocation');
124
        $isMultiple = $element->getAttribute('multiple', false);
0 ignored issues
show
Unused Code introduced by
The call to ElementInterface::getAttribute() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
125
126
        $values = [];
127
        foreach ($this->object->getLocations() as $loc) {
128
            $values[] = trim(
129
                $loc->getPostalCode() . ' ' . $loc->getCity() . ', ' . $loc->getRegion(),
130
                ' ,'
131
            );
132
        }
133
134
        if (count($values)) {
135
            if ($isMultiple) {
136
                return '<ul><li>' . join('</li><li>', $values) . '</li></ul>';
137
            } else {
138
                return $values[0];
139
            }
140
        } else {
141
            return '';
142
        }
143
    }
144
}
145
146