Failed Conditions
Pull Request — experimental/3.1 (#2486)
by
unknown
122:07 queued 114:48
created

StrictArrayCollection::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 8
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Common\Collection;
25
26
use Closure;
27
use Doctrine\Common\Collections\ArrayCollection;
28
29
abstract class StrictArrayCollection extends ArrayCollection
0 ignored issues
show
introduced by
Abstract class name is not prefixed with "Abstract"
Loading history...
introduced by
Missing class doc comment
Loading history...
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34 37
    public function __construct(array $array = array())
35
    {
36 37
        parent::__construct();
37
38 37
        foreach ($array as $key => $value) {
39 32
            $this->set($key, $value);
40
        }
41
    }
42
43
    /**
44
     * @param mixed $element
45
     * @return bool
46
     */
47
    abstract public function checkValue($element);
48
49
    /**
50
     * {@inheritdoc}
51
     * @throws \InvalidArgumentException
52
     */
53 32 View Code Duplication
    public function set($key, $value)
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...
54
    {
55 32
        if (!$this->checkValue($value)) {
56
            throw new \InvalidArgumentException(sprintf("%s does not accept the passed element.", get_class($this)));
57
        }
58
59 32
        parent::set($key, $value);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     * @throws \InvalidArgumentException
65
     */
66 4 View Code Duplication
    public function add($value)
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...
67
    {
68 4
        if (!$this->checkValue($value)) {
69 1
            throw new \InvalidArgumentException(sprintf('%s does not accept the passed element.', get_class($this)));
70
        }
71
72 3
        return parent::add($value);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function map(Closure $func)
79
    {
80
        return new parent(array_map($func, $this->toArray()));
81
    }
82
83
    /**
84
     * 型を保証するmap
85
     *
86
     * @param Closure $func
87
     * @return static
88
     */
89
    public function strictMap(Closure $func)
90
    {
91
        $collection = $this;
92
93
        $newFunc = function ($element) use ($collection, $func) {
94
95
            $new = $func($element);
96
97
            if (!$collection->checkValue($new)) {
98
                throw new \InvalidArgumentException(sprintf('Element converted to unacceptable value. Use %s::looseMap(Closure) instead.', get_class($collection)));
99
            }
100
101
            return $new;
102
        };
103
104
        return parent::map($newFunc);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (map() instead of strictMap()). Are you sure this is correct? If so, you might want to change this to $this->map().

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...
105
    }
106
}