Completed
Push — 15.x ( 39dd4d...e89609 )
by Tim
02:40
created

ColumnMetadataLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Loaders\ColumnMetadataLoader
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 2020 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
use TechDivision\Import\Connection\ConnectionInterface;
24
25
/**
26
 * Loader for table column metadata.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2020 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class ColumnMetadataLoader implements LoaderInterface
35
{
36
37
    /**
38
     * The column metadata.
39
     *
40
     * @var array
41
     */
42
    protected $metadata = array();
43
44
    /**
45
     * Construct a new instance.
46
     *
47
     * @param \TechDivision\Import\Connection\ConnectionInterface $connection The DB connection instance used to load the table metadata
48
     */
49
    public function __construct(ConnectionInterface $connection)
50
    {
51
52
        // initialize the statement to load the table columns
53
        $stmtTables = $connection->query('SHOW TABLES');
54
55
        // fetch the tables names of the actual database
56
        $tables = $stmtTables->fetchAll(\PDO::FETCH_ASSOC);
57
58
        // iterate over the rows with the tables
59
        foreach ($tables as $table) {
60
            // extract the tablename
61
            $tableName = reset($table);
62
            // load the table's columns
63
            $stmtColumns = $connection->query(sprintf('SHOW COLUMNS FROM %s', $tableName));
64
            $columns = $stmtColumns->fetchAll(\PDO::FETCH_ASSOC);
65
            // prepare the metadata
66
            foreach ($columns as $column) {
67
                $this->metadata[$tableName][$column['Field']] = $column;
68
            }
69
        }
70
    }
71
72
    /**
73
     * Loads and returns tables metadata.
74
     *
75
     * @param string $tableName The table name to return the metadata for
76
     *
77
     * @return \ArrayAccess The array with the metadata
78
     */
79
    public function load($tableName = null)
80
    {
81
82
        // query whether or not metadata for the table is available
83
        if (isset($this->metadata[$tableName])) {
84
            return $this->metadata[$tableName];
85
        }
86
87
        // return an empty array otherwise
88
        return array();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (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...
89
    }
90
}
91