Completed
Push — master ( 3a08fe...aee93b )
by Tim
18:05 queued 16:13
created

GenericMemberNameLoader::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
/**
4
 * TechDivision\Import\Loaders\GenericMemberNameLoader
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2021 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Loaders;
22
23
/**
24
 * Generic loader iplementation that loads the values of the member with
25
 * the given name of the entities laoded by the parent loader instance.
26
 *
27
 * @author    Tim Wagner <[email protected]>
28
 * @copyright 2021 TechDivision GmbH <[email protected]>
29
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
30
 * @link      https://github.com/techdivision/import
31
 * @link      http://www.techdivision.com
32
 */
33
class GenericMemberNameLoader implements LoaderInterface
34
{
35
36
    /**
37
     * The array with the member values.
38
     *
39
     * @var array
40
     */
41
    protected $values;
42
43
    /**
44
     * Construct that initializes the iterator with the parent iterator instance used to load the enities.
45
     *
46
     * @param \TechDivision\Import\Loaders\LoaderInterface $entityLoader The entity loader instance
47
     * @param string                                       $memberName   The name of the entity's member to load the values for
48
     *
49
     * @throws \InvalidArgumentException Is thrown, if one of the loaded entities doesn't contain the member with the given name
50
     */
51
    public function __construct(LoaderInterface $entityLoader, string $memberName)
52
    {
53
54
        // load the entities
55
        $entities = $entityLoader->load();
56
57
        // load the member values
58
        foreach ($entities as $entity) {
59
            if (isset($entity[$memberName])) {
60
                $this->values[] = $entity[$memberName];
61
            } else {
62
                throw new \InvalidArgumentException(sprintf('Mandatory member "%s" not available', $memberName));
63
            }
64
        }
65
    }
66
67
    /**
68
     * Loads and returns data.
69
     *
70
     * @return \ArrayAccess The array with the data
71
     */
72
    public function load()
73
    {
74
        return $this->values;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->values; (array) is incompatible with the return type declared by the interface TechDivision\Import\Loaders\LoaderInterface::load of type ArrayAccess.

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...
75
    }
76
}
77