Issues (4)

src/Link/Sort.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Cocur\Chain\Link;
4
5
/**
6
 * Class Sort.
7
 *
8
 * @author      Christoph Rosse
9
 * @author      Florian Eckerstorfer <[email protected]>
10
 */
11
trait Sort
12
{
13
    /**
14
     * Sort a Chain.
15
     *
16
     * @param int|callable $sortBy
17
     * @param array        $options
18
     *
19
     * @return self
20
     */
21 8
    public function sort($sortBy = SORT_REGULAR, array $options = []): self
22
    {
23 8
        if (!$sortBy) {
24 4
            $sortBy = SORT_REGULAR;
25
        }
26 8
        if ($sortBy && is_callable($sortBy)) {
27 2
            $this->sortWithCallback($sortBy, $options);
0 ignored issues
show
It seems like $sortBy can also be of type integer; however, parameter $callback of Cocur\Chain\Link\Sort::sortWithCallback() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
            $this->sortWithCallback(/** @scrutinizer ignore-type */ $sortBy, $options);
Loading history...
28
        } else {
29 6
            $this->sortWithFlags($sortBy, $options);
0 ignored issues
show
It seems like $sortBy can also be of type callable; however, parameter $sortFlags of Cocur\Chain\Link\Sort::sortWithFlags() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
            $this->sortWithFlags(/** @scrutinizer ignore-type */ $sortBy, $options);
Loading history...
30
        }
31
32 8
        return $this;
33
    }
34
35
    /**
36
     * @param callable $callback
37
     * @param array    $options
38
     */
39 2
    private function sortWithCallback(callable $callback, array $options = []): void
40
    {
41 2
        if (isset($options['assoc']) && $options['assoc']) {
42 1
            uasort($this->array, $callback);
43
        } else {
44 1
            usort($this->array, $callback);
45
        }
46 2
    }
47
48
    /**
49
     * @param int   $sortFlags
50
     * @param array $options
51
     */
52 6
    private function sortWithFlags(int $sortFlags = SORT_REGULAR, array $options = []): void
53
    {
54 6
        if (!empty($options['assoc']) && !empty($options['reverse'])) {
55 1
            arsort($this->array, $sortFlags);
56 5
        } elseif (!empty($options['assoc'])) {
57 1
            asort($this->array, $sortFlags);
58 4
        } elseif (!empty($options['reverse'])) {
59 1
            rsort($this->array, $sortFlags);
60
        } else {
61 3
            sort($this->array, $sortFlags);
62
        }
63 6
    }
64
}
65