Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

SortedLoader::getLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Loaders\SortedFilesystemLoader
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2020 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Loaders;
22
23
use TechDivision\Import\Loaders\Sorters\UasortImpl;
24
use TechDivision\Import\Loaders\Sorters\SorterImplInterface;
25
26
/**
27
 * Generic loader implementation that uses a glob compatible pattern
28
 * to load files from a given directory.
29
 *
30
 * The loader uses the PHP function `uasort` to sort the files, so take
31
 * into account, that the keys of the array will be kept after sorting.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2020 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import
37
 * @link      http://www.techdivision.com
38
 * @link      https://www.php.net/uasort
39
 */
40
class SortedLoader implements SortedLoaderInterface
41
{
42
43
    /**
44
     * The parent loader instance.
45
     *
46
     * @var \TechDivision\Import\Loaders\LoaderInterface
47
     */
48
    private $loader;
49
50
    /**
51
     * The sorter instance to use.
52
     *
53
     * @var \TechDivision\Import\Loaders\Sorters\SorterImplInterface
54
     */
55
    private $sorterImpl;
56
57
    /**
58
     * Construct that initializes the loader with the parent loader instance.
59
     *
60
     * @param \TechDivision\Import\Loaders\LoaderInterface                  $loader     The parent loader instance
61
     * @param \TechDivision\Import\Loaders\Sorters\SorterImplInterface|null $sorterImpl The sorter instance to use
62
     */
63
    public function __construct(LoaderInterface $loader, SorterImplInterface $sorterImpl = null)
64
    {
65
        $this->loader = $loader;
66
        $this->sorterImpl = $sorterImpl ?? new UasortImpl();
67
    }
68
69
    /**
70
     * Add's the passed sorter to the loader instance.
71
     *
72
     * @param callable $sorter The sorter to add
73
     *
74
     * @return void
75
     */
76
    public function addSorter(callable $sorter) : void
77
    {
78
        $this->getSorterImpl()->addSorter($sorter);
79
    }
80
81
    /**
82
     * Return's the array with the sorter callbacks.
83
     *
84
     * @return callable[] The sorter callbacks
85
     */
86
    public function getSorters() : array
87
    {
88
        return $this->getSorterImpl()->getSorters();
89
    }
90
91
    /**
92
     * Loads, sorts and returns the files by using the passed glob pattern.
93
     *
94
     * If no pattern will be passed to the `load()` method, the files of
95
     * the actual directory using `getcwd()` will be returned.
96
     *
97
     * @param string|null $pattern The pattern to load the files from the filesystem
98
     *
99
     * @return \ArrayAccess The array with the data
100
     */
101
    public function load(string $pattern = null) : \ArrayAccess
102
    {
103
104
        // sort the files loaded by the parent loader instance
105
        $this->getSorterImpl()->sort($data = $this->getLoader()->load($pattern));
0 ignored issues
show
Bug introduced by
$data = $this->getLoader()->load($pattern) cannot be passed to sort() as the parameter $data expects a reference.
Loading history...
Unused Code introduced by
The call to LoaderInterface::load() has too many arguments starting with $pattern.

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...
Documentation introduced by
$data = $this->getLoader()->load($pattern) is of type object<ArrayAccess>, but the function expects a array.

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...
106
107
        // return the sorted files
108
        return $data;
109
    }
110
111
    /**
112
     * Return's the sorter instance to use.
113
     *
114
     * @return \TechDivision\Import\Loaders\Sorters\SorterImplInterface The sorter instance to use
115
     */
116
    protected function getSorterImpl() : SorterImplInterface
117
    {
118
        return $this->sorterImpl;
119
    }
120
121
    /**
122
     * Return's the parent loader instance.
123
     *
124
     * @return \TechDivision\Import\Loaders\LoaderInterface The parent loader instance
125
     */
126
    protected function getLoader() : LoaderInterface
127
    {
128
        return $this->loader;
129
    }
130
}
131