Completed
Push — 16.x ( 0056bb...bd3602 )
by Tim
02:11 queued 10s
created

SortedLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 91
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addSorter() 0 4 1
A getSorters() 0 4 1
A load() 0 9 1
A getSorterImpl() 0 4 1
A getLoader() 0 4 1
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
24
use TechDivision\Import\Loaders\Sorters\UasortImpl;
25
use TechDivision\Import\Loaders\Sorters\SorterImplInterface;
26
27
/**
28
 * Generic loader implementation that uses a glob compatible pattern
29
 * to load files from a given directory.
30
 *
31
 * The loader uses the PHP function `uasort` to sort the files, so take
32
 * into account, that the keys of the array will be kept after sorting.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2020 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/techdivision/import
38
 * @link      http://www.techdivision.com
39
 * @link      https://www.php.net/uasort
40
 */
41
class SortedLoader implements SortedLoaderInterface
42
{
43
44
    /**
45
     * The parent loader instance.
46
     *
47
     * @var \TechDivision\Import\Loaders\LoaderInterface
48
     */
49
    private $loader;
50
51
    /**
52
     * The sorter instance to use.
53
     *
54
     * @var \TechDivision\Import\Loaders\Sorters\SorterImplInterface
55
     */
56
    private $sorterImpl;
57
58
    /**
59
     * Construct that initializes the loader with the parent loader instance.
60
     *
61
     * @param \TechDivision\Import\Loaders\LoaderInterface                  $loader     The parent loader instance
62
     * @param \TechDivision\Import\Loaders\Sorters\SorterImplInterface|null $sorterImpl The sorter instance to use
63
     */
64
    public function __construct(LoaderInterface $loader, SorterImplInterface $sorterImpl = null)
65
    {
66
        $this->loader = $loader;
67
        $this->sorterImpl = $sorterImpl ?? new UasortImpl();
68
    }
69
70
    /**
71
     * Add's the passed sorter to the loader instance.
72
     *
73
     * @param callable $sorter The sorter to add
74
     *
75
     * @return void
76
     */
77
    public function addSorter(callable $sorter) : void
78
    {
79
        $this->getSorterImpl()->addSorter($sorter);
80
    }
81
82
    /**
83
     * Return's the array with the sorter callbacks.
84
     *
85
     * @return callable[] The sorter callbacks
86
     */
87
    public function getSorters() : array
88
    {
89
        return $this->getSorterImpl()->getSorters();
90
    }
91
92
    /**
93
     * Loads, sorts and returns the files by using the passed glob pattern.
94
     *
95
     * If no pattern will be passed to the `load()` method, the files of
96
     * the actual directory using `getcwd()` will be returned.
97
     *
98
     * @param string|null $pattern The pattern to load the files from the filesystem
99
     *
100
     * @return \ArrayAccess The array with the data
101
     */
102
    public function load(string $pattern = null) : \ArrayAccess
103
    {
104
105
        // sort the files loaded by the parent loader instance
106
        $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...
107
108
        // return the sorted files
109
        return $data;
110
    }
111
112
    /**
113
     * Return's the sorter instance to use.
114
     *
115
     * @return \TechDivision\Import\Loaders\Sorters\SorterImplInterface The sorter instance to use
116
     */
117
    protected function getSorterImpl() : SorterImplInterface
118
    {
119
        return $this->sorterImpl;
120
    }
121
122
    /**
123
     * Return's the parent loader instance.
124
     *
125
     * @return \TechDivision\Import\Loaders\LoaderInterface The parent loader instance
126
     */
127
    protected function getLoader() : LoaderInterface
128
    {
129
        return $this->loader;
130
    }
131
}
132