Completed
Push — master ( 785bde...8b4d11 )
by Freek
01:29
created

ParallelMap::__invoke()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 1
nop 0
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
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
 * @return \Illuminate\Support\Collection
19
 */
20
class ParallelMap
21
{
22
    public function __invoke()
23
    {
24
        return function (callable $callback, $workers = null): Collection {
25
            $pool = null;
26
27
            if ($workers instanceof Pool) {
28
                $pool = $workers;
29
            }
30
31
            if (is_int($workers)) {
32
                $pool = new DefaultPool($workers);
33
            }
34
35
            $promises = parallelMap($this->items, $callback, $pool);
0 ignored issues
show
Bug introduced by
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...
36
37
            return new static(wait($promises));
0 ignored issues
show
Unused Code introduced by
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...
38
        };
39
    }
40
}
41