ParallelMap   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 3
1
<?php
2
3
namespace Spatie\CollectionMacros\Macros;
4
5
use Amp\Parallel\Worker\Pool;
6
use function Amp\Promise\wait;
7
use Illuminate\Support\Collection;
8
use Amp\Parallel\Worker\DefaultPool;
9
use function Amp\ParallelFunctions\parallelMap;
0 ignored issues
show
Bug introduced by Lucas Michot
This use statement conflicts with another class in this namespace, Spatie\CollectionMacros\Macros\parallelMap.

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...
10
11
/**
12
 * Idential to map but each item will be processed in parallel.
13
 *
14
 * This function requires the installation of amphp/parallel-functions
15
 *
16
 * @param callable $callback
17
 *
18
 * @mixin \Illuminate\Support\Collection
19
 *
20
 * @return \Illuminate\Support\Collection
21
 */
22
class ParallelMap
23
{
24
    public function __invoke()
25
    {
26
        return function (callable $callback, $workers = null): Collection {
27
            $pool = null;
28
29
            if ($workers instanceof Pool) {
30
                $pool = $workers;
31
            }
32
33
            if (is_int($workers)) {
34
                $pool = new DefaultPool($workers);
35
            }
36
37
            $promises = parallelMap($this->items, $callback, $pool);
0 ignored issues
show
Bug introduced by Lucas Michot
The property items does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
39
            return new static(wait($promises));
0 ignored issues
show
Unused Code introduced by Lucas Michot
The call to ParallelMap::__construct() has too many arguments starting with \Amp\Promise\wait($promises).

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...
40
        };
41
    }
42
}
43