Completed
Push — 8.x ( 824af6 )
by Tim
09:11
created

ValueCsvSerializerFactory::createSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 9.9
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Serializers\ValueCsvSerializerFactory
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 2018 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\Serializers;
22
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use TechDivision\Import\Configuration\CsvConfigurationInterface;
25
use TechDivision\Import\Utils\DependencyInjectionKeys;
26
27
/**
28
 * The factory implementation for CSV value serializer instances.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2018 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36 View Code Duplication
class ValueCsvSerializerFactory implements ConfigurationAwareSerializerFactoryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
{
38
39
    /**
40
     * The DI container instance.
41
     *
42
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
43
     */
44
    protected $container;
45
46
    /**
47
     * Initialize the factory with the DI container instance.
48
     *
49
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance
50
     */
51
    public function __construct(ContainerInterface $container)
52
    {
53
        $this->container = $container;
54
    }
55
56
    /**
57
     * Creates and returns the serializer instance.
58
     *
59
     * @param \TechDivision\Import\Configuration\CsvConfigurationInterface $configuration The CSV configuration
60
     *
61
     * @return \TechDivision\Import\Serializers\SerializerInterface The serializer instance
62
     */
63
    public function createSerializer(CsvConfigurationInterface $configuration)
64
    {
65
66
        // load the serializer instance from the container and pass the configuration
67
        /** @var \TechDivision\Import\Serializers\ValueCsvSerializerInterface $serializer */
68
        $serializer = $this->container->get(DependencyInjectionKeys::IMPORT_SERIALIZER_CSV_VALUE);
69
        $serializer->init($configuration);
70
71
        // return the serializer instance
72
        return $serializer;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $serializer; (TechDivision\Import\Seri...eCsvSerializerInterface) is incompatible with the return type declared by the interface TechDivision\Import\Seri...rface::createSerializer of type TechDivision\Import\Seri...wareSerializerInterface.

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...
73
    }
74
}
75