Completed
Pull Request — master (#81)
by Tim
04:12
created

ExportConfigFactory::createExportConfig()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 39
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 23
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 64
nop 0
crap 56
1
<?php
2
3
/**
4
 * TechDivision\Import\Adapter\Csv\ExportConfigFactory
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 2016 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\Adapter\Csv;
22
23
use Goodby\CSV\Export\Standard\ExporterConfig;
24
use TechDivision\Import\ConfigurationInterface;
25
26
/**
27
 * Factory implementation for a CSV export configuration.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class ExportConfigFactory implements ExportConfigFactoryInterface
36
{
37
38
    /**
39
     * The configuration instance.
40
     *
41
     * @var \TechDivision\Import\Configuration\ConfigurationInterface
42
     */
43
    protected $configuration;
44
45
    /**
46
     * Initialize the adapter with the configuration.
47
     *
48
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
49
     */
50
    public function __construct(ConfigurationInterface $configuration)
51
    {
52
        $this->configuration = $configuration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configuration of type object<TechDivision\Impo...ConfigurationInterface> is incompatible with the declared type object<TechDivision\Impo...ConfigurationInterface> of property $configuration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
    }
54
55
    /**
56
     * Factory method to create a new export configuration instance.
57
     *
58
     * @return \Goodby\CSV\Export\Standard\ExportConfig The export configuration
59
     */
60
    public function createExportConfig()
61
    {
62
63
        // initialize the lexer configuration
64
        $config = new ExporterConfig();
65
66
        // query whether or not a delimiter character has been configured
67
        if ($delimiter = $this->configuration->getDelimiter()) {
68
            $config->setDelimiter($delimiter);
69
        }
70
71
        // query whether or not a custom escape character has been configured
72
        if ($escape = $this->configuration->getEscape()) {
73
            $config->setEscape($escape);
74
        }
75
76
        // query whether or not a custom enclosure character has been configured
77
        if ($enclosure = $this->configuration->getEnclosure()) {
78
            $config->setEnclosure($enclosure);
79
        }
80
81
        // query whether or not a custom source charset has been configured
82
        if ($fromCharset = $this->configuration->getFromCharset()) {
83
            $config->setFromCharset($fromCharset);
84
        }
85
86
        // query whether or not a custom target charset has been configured
87
        if ($toCharset = $this->configuration->getToCharset()) {
88
            $config->setToCharset($toCharset);
89
        }
90
91
        // query whether or not a custom file mode has been configured
92
        if ($fileMode = $this->configuration->getFileMode()) {
93
            $config->setFileMode($fileMode);
94
        }
95
96
        // return the lexer configuration
97
        return $config;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $config; (Goodby\CSV\Export\Standard\ExporterConfig) is incompatible with the return type declared by the interface TechDivision\Import\Adap...ace::createExportConfig of type Goodby\CSV\Export\Standard\ExportConfig.

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...
98
    }
99
}
100