OptionsResolver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 86.96%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 10
c 2
b 1
f 1
lcom 1
cbo 1
dl 0
loc 65
ccs 20
cts 23
cp 0.8696
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefined() 0 8 2
A setTypesAllowed() 0 12 3
A setNormalizers() 0 12 3
A isLatest() 0 8 2
1
<?php
2
3
namespace Crummy\Phlack\Common;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver as BaseOptionsResolver;
6
7
class OptionsResolver extends BaseOptionsResolver
8
{
9
    private $isLatestVersion;
10
11
    /**
12
     * Set a list of allowed types for the given option.
13
     *
14
     * @see Symfony\Component\OptionsResolver\OptionsResolver::setAllowedTypes()
15
     *
16
     * @param array $allowedTypes
17
     *
18
     * @return $this
19
     */
20 30
    public function setTypesAllowed($allowedTypes = null)
21
    {
22 30
        if (!$this->isLatest()) {
23
            return $this->setAllowedTypes($allowedTypes);
0 ignored issues
show
Bug introduced by
The call to setAllowedTypes() misses a required argument $allowedTypes.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$allowedTypes is of type array|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
        }
25
26 30
        foreach ($allowedTypes as $option => $typesAllowed) {
0 ignored issues
show
Bug introduced by
The expression $allowedTypes of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
27 30
            $this->setAllowedTypes($option, $typesAllowed);
28 30
        }
29
30 30
        return $this;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 75
    public function setDefined($optionNames)
37
    {
38 75
        if (!$this->isLatest()) {
39
            return $this->setOptional($optionNames);
0 ignored issues
show
Bug introduced by
The method setOptional() does not seem to exist on object<Crummy\Phlack\Common\OptionsResolver>.

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...
40
        }
41
42 75
        return parent::setDefined($optionNames);
43
    }
44
45 22
    public function setNormalizers(array $normalizers)
46
    {
47 22
        if (!$this->isLatest()) {
48
            return parent::setNormalizers($normalizers);
0 ignored issues
show
Bug introduced by
The method setNormalizers() does not exist on Symfony\Component\OptionsResolver\OptionsResolver. Did you maybe mean setNormalizer()?

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...
49
        }
50
51 22
        foreach ($normalizers as $option => $normalizer) {
52 22
            parent::setNormalizer($option, $normalizer);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setNormalizer() instead of setNormalizers()). Are you sure this is correct? If so, you might want to change this to $this->setNormalizer().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
53 22
        }
54
55 22
        return $this;
56
    }
57
58
    /**
59
     * Checks whether or not the OptionsResolver version is up-to-date.
60
     *
61
     * @return bool
62
     */
63 76
    private function isLatest()
64
    {
65 76
        if (null === $this->isLatestVersion) {
66 76
            $this->isLatestVersion = method_exists(current(class_parents($this)), 'setDefined');
67 76
        }
68
69 76
        return $this->isLatestVersion;
70
    }
71
}
72