Failed Conditions
Pull Request — experimental/3.1 (#2486)
by
unknown
68:04 queued 25:47
created

StrictArrayCollection::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 8
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
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
    final 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
    final 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
     * @throws \InvalidArgumentException
78
     */
79
    final public function map(Closure $func)
80
    {
81
        $collection = $this;
82
83
        $newFunc = function ($element) use ($collection, $func) {
84
85
            $new = $func($element);
86
87
            if (!$collection->checkValue($new)) {
88
                throw new \InvalidArgumentException(sprintf('Element converted to unacceptable value. Use %s::looseMap(Closure) instead.', get_class($collection)));
89
            }
90
91
            return $new;
92
        };
93
94
        return parent::map($newFunc);
95
    }
96
97
    /**
98
     * 型を保証しないmap
99
     *
100
     * @param Closure $func
101
     * @return ArrayCollection
102
     */
103
    final public function looseMap(Closure $func)
104
    {
105
        return new parent(array_map($func, $this->toArray()));
106
    }
107
}