Sort::__invoke()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 64
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 5.2385

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 26
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 64
ccs 26
cts 33
cp 0.7879
crap 5.2385
rs 9.1928

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use Exception;
9
use Generator;
10
use loophp\collection\Contract\Operation;
11
use loophp\iterators\SortIterableAggregate;
12
13
/**
14
 * @immutable
15
 *
16
 * @template TKey
17
 * @template T
18
 */
19
final class Sort extends AbstractOperation
20
{
21
    /**
22
     * @return Closure(int): Closure(null|(Closure(T, T, TKey, TKey): int)): Closure(iterable<TKey, T>): Generator<TKey, T>
23
     */
24 16
    public function __invoke(): Closure
25
    {
26 16
        return
27
            /**
28
             * @return Closure(null|Closure(T, T, TKey, TKey): int): Closure(iterable<TKey, T>): Generator<TKey, T>
29
             */
30 16
            static fn (int $type = Operation\Sortable::BY_VALUES): Closure =>
31
                /**
32
                 * @param null|(Closure(T, T, TKey, TKey): int)|(callable(T, T, TKey, TKey): int) $callback
0 ignored issues
show
Documentation Bug introduced by
The doc comment null|(Closure(T, T, TKey...T, T, TKey, TKey): int) at position 3 could not be parsed: Expected ')' at position 3, but found 'Closure'.
Loading history...
33
                 *
34
                 * @return Closure(iterable<TKey, T>): Generator<TKey, T>
35
                 */
36 16
                static function (null|callable|Closure $callback = null) use ($type): Closure {
37 16
                    if (Operation\Sortable::BY_VALUES !== $type && Operation\Sortable::BY_KEYS !== $type) {
38 2
                        throw new Exception('Invalid sort type.');
39
                    }
40
41 14
                    $callback ??=
42
                        /**
43
                         * @param T $left
44
                         * @param T $right
45
                         * @param TKey $leftKey
46
                         * @param TKey $rightKey
0 ignored issues
show
Bug introduced by
The type loophp\collection\Operation\TKey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
                         */
48 14
                        static fn (mixed $left, mixed $right, mixed $leftKey, mixed $rightKey): int => $left <=> $right;
0 ignored issues
show
Unused Code introduced by
The parameter $rightKey is not used and could be removed. ( Ignorable by Annotation )

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

48
                        static fn (mixed $left, mixed $right, mixed $leftKey, /** @scrutinizer ignore-unused */ mixed $rightKey): int => $left <=> $right;

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $leftKey is not used and could be removed. ( Ignorable by Annotation )

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

48
                        static fn (mixed $left, mixed $right, /** @scrutinizer ignore-unused */ mixed $leftKey, mixed $rightKey): int => $left <=> $right;

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
50 14
                    if (!($callback instanceof Closure)) {
51
                        trigger_deprecation(
52
                            'loophp/collection',
53
                            '7.4',
54
                            'Passing a callable as argument is deprecated and will be removed in 8.0. Use a closure instead.',
55
                            self::class
56
                        );
57
58
                        $callback = Closure::fromCallable($callback);
59
                    }
60
61 14
                    $operations = Operation\Sortable::BY_VALUES === $type ?
62 10
                        [
63 10
                            'before' => [],
64 10
                            'after' => [],
65 10
                        ] :
66 4
                        [
67 4
                            'before' => [(new Flip())()],
68 4
                            'after' => [(new Flip())()],
69 4
                        ];
70
71 14
                    $sortedIterator =
72
                        /**
73
                         * @param iterable<TKey, T> $iterable
74
                         *
75
                         * @return SortIterableAggregate<TKey, T>
76
                         */
77 14
                        static fn (iterable $iterable): SortIterableAggregate => new SortIterableAggregate($iterable, $callback);
78
79
                    /** @var Closure(iterable<TKey, T>): Generator<TKey, T> $sort */
80 14
                    $sort = (new Pipe())()(
81 14
                        ...$operations['before'],
82 14
                        ...[$sortedIterator],
83 14
                        ...$operations['after']
84 14
                    );
85
86
                    // Point free style.
87 14
                    return $sort;
88 16
                };
89
    }
90
}
91