Completed
Push — master ( 3bd5ac...d91a6c )
by Lars
01:37
created

DetectFirstValueTypeCollection::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arrayy\Type;
6
7
use function Arrayy\array_first;
8
use Arrayy\Arrayy;
9
use Arrayy\ArrayyIterator;
10
use Arrayy\Collection\Collection;
11
12
/**
13
 * @template T
14
 * @extends Collection<T>
15
 */
16
final class DetectFirstValueTypeCollection extends Collection implements TypeInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $getTypeHelper;
22
23
    /**
24
     * @param array|Arrayy $data
25
     * @param string       $iteratorClass
26
     * @param bool         $checkPropertiesInConstructor
27
     *
28
     * @psalm-param array<T>|Arrayy $data
29
     * @psalm-param class-string<\ArrayIterator> $iteratorClass
30
     */
31 8
    public function __construct(
32
        $data = [],
33
        string $iteratorClass = ArrayyIterator::class,
34
        bool $checkPropertiesInConstructor = true
35
    ) {
36 8
        if ($data instanceof Arrayy) {
37 2
            $firstValue = $data->first();
38 6
        } elseif (\is_array($data)) {
39 6
            $firstValue = array_first($data);
40
        } else {
41
            $firstValue = $data;
42
            $data = [$data];
43
        }
44
45 8
        $this->getTypeHelper = $this->getTypeFromFirstValue($firstValue);
46
47 8
        parent::__construct(
48 8
            $data,
49 8
            $iteratorClass,
50 8
            $checkPropertiesInConstructor
51
        );
52 5
    }
53
54
    /**
55
     * The type (FQCN) associated with this collection.
56
     *
57
     * @return string
58
     */
59 8
    public function getType()
60
    {
61 8
        return $this->getTypeHelper;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getTypeHelper; (string) is incompatible with the return type of the parent method Arrayy\Collection\Collection::getType of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
62
    }
63
64
    /**
65
     * @param mixed $value
66
     *
67
     * @return string
68
     *
69
     * @psalm-param T $value
70
     */
71 8
    private function getTypeFromFirstValue($value): string
72
    {
73 8
        return \is_object($value) ? \get_class($value) : \gettype($value);
74
    }
75
}
76