Completed
Push — 1.x ( ae684c...0fa590 )
by Théo
01:16
created

SplDoublyLinkedListFilter::createCopyClosure()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.4285
1
<?php
2
3
namespace DeepCopy\TypeFilter\Spl;
4
5
use Closure;
6
use DeepCopy\DeepCopy;
7
use DeepCopy\TypeFilter\TypeFilter;
8
use SplDoublyLinkedList;
0 ignored issues
show
Bug introduced by Théo FIDRY
This use statement conflicts with another class in this namespace, DeepCopy\TypeFilter\Spl\SplDoublyLinkedList.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
10
/**
11
 * @final
12
 */
13
class SplDoublyLinkedListFilter implements TypeFilter
14
{
15
    /**
16
     * @var DeepCopy
17
     */
18
    private $copier;
19
20
    public function __construct(DeepCopy $copier)
21
    {
22
        $this->copier = $copier;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function apply($element)
29
    {
30
        $newElement = clone $element;
31
32
        $copy = $this->createCopyClosure();
33
34
        return $copy($newElement);
35
    }
36
37
    private function createCopyClosure()
38
    {
39
        $copier = $this->copier;
40
41
        $copy = function (SplDoublyLinkedList $list) use ($copier) {
42
            // Replace each element in the list with a deep copy of itself
43
            for ($i = 1; $i <= $list->count(); $i++) {
44
                $copy = $copier->recursiveCopy($list->shift());
0 ignored issues
show
Bug introduced by Théo FIDRY
The method recursiveCopy() cannot be called from this context as it is declared private in class DeepCopy\DeepCopy.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
45
46
                $list->push($copy);
47
            }
48
49
            return $list;
50
        };
51
52
        return Closure::bind($copy, null, DeepCopy::class);
53
    }
54
}
55