Passed
Pull Request — 2.x (#344)
by Pol
20:13
created

LoophpCollectionFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A withCollectionClass() 0 5 2
A __construct() 0 7 2
A getInterface() 0 3 1
A collect() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Collection;
6
7
use Cycle\ORM\Exception\CollectionFactoryException;
8
use loophp\collection\Collection;
0 ignored issues
show
Bug introduced by
The type loophp\collection\Collection 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...
9
use loophp\collection\Contract\Collection as CollectionInterface;
0 ignored issues
show
Bug introduced by
The type loophp\collection\Contract\Collection 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...
10
11
/**
12
 * @template TCollection of Collection
13
 * @template-implements CollectionFactoryInterface<TCollection>
14
 */
15
final class LoophpCollectionFactory implements CollectionFactoryInterface
16
{
17
    /** @var class-string<TCollection> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TCollection> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TCollection>.
Loading history...
18
    private string $class = Collection::class;
19
20
    public function __construct()
21
    {
22
        if (!class_exists(Collection::class, true)) {
23
            throw new CollectionFactoryException(
24
                sprintf(
25
                    'There is no %s class. To resolve this issue you can install `loophp/collection` package.',
26
                    Collection::class
27
                )
28
            );
29
        }
30
    }
31
32
    public function getInterface(): ?string
33
    {
34
        return CollectionInterface::class;
35
    }
36
37
    public function withCollectionClass(string $class): static
38
    {
39
        $clone = clone $this;
40
        $clone->class = $class === Collection::class ? Collection::class : $class;
41
        return $clone;
42
    }
43
44
    public function collect(iterable $data): iterable
45
    {
46
        return ($this->class === Collection::class)
47
            ? Collection::fromIterable($data)
48
            : new $this->class($data);
49
    }
50
}
51