AbstractAnalyzer::getDatabaseConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Analyzer abstraction
4
 *
5
 * @package CacheCheck\Service\Analyzer
6
 * @author  Tim Lochmüller
7
 */
8
9
namespace HDNET\CacheCheck\Service\Analyzer;
10
11
use HDNET\CacheCheck\Exception;
12
use TYPO3\CMS\Core\Database\DatabaseConnection;
13
use TYPO3\CMS\Core\SingletonInterface;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
16
/**
17
 * Analyzer abstraction
18
 *
19
 * @author Tim Lochmüller
20
 */
21
abstract class AbstractAnalyzer implements AnalyzerInterface, SingletonInterface
22
{
23
24
    /**
25
     * get dynamic values from database
26
     *
27
     * @param array $queryValues
28
     *
29
     * @return string|null
30
     */
31
    protected function getDynamicFromDatabase(array $queryValues)
32
    {
33
        $databaseConnection = $this->getDatabaseConnection();
34
        $checkValues = [
35
            'SELECT',
36
            'FROM',
37
            'WHERE',
38
            'GROUPBY',
39
            'ORDERBY',
40
            'LIMIT'
41
        ];
42
        foreach ($checkValues as $name) {
43
            if (!array_key_exists($name, $queryValues)) {
44
                $queryValues[$name] = '';
45
            }
46
        }
47
48
        $res = $databaseConnection->exec_SELECT_queryArray($queryValues);
49
        if (!$res) {
50
            return new Exception('Invalid SQL Query: ' . var_export($checkValues, true), 2346273846783);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \HDNET\CacheC... true), 2346273846783); (HDNET\CacheCheck\Exception) is incompatible with the return type documented by HDNET\CacheCheck\Service...:getDynamicFromDatabase of type string|null.

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...
51
        }
52
        $result = $databaseConnection->sql_fetch_row($res);
53
        if (!isset($result[0])) {
54
            return new Exception('No single value is found in the SQL Query', 324528943578);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \HDNET\CacheC... Query', 324528943578); (HDNET\CacheCheck\Exception) is incompatible with the return type documented by HDNET\CacheCheck\Service...:getDynamicFromDatabase of type string|null.

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...
55
        }
56
        return $result[0];
57
    }
58
59
    /**
60
     * Get an analyzer
61
     *
62
     * @param string $name
63
     *
64
     * @return AnalyzerInterface
65
     */
66
    protected function getAnalyzer($name)
67
    {
68
        return GeneralUtility::makeInstance('HDNET\\CacheCheck\\Service\\Analyzer\\' . $name);
69
    }
70
71
    /**
72
     * Get database connection
73
     *
74
     * @return DatabaseConnection
75
     */
76
    protected function getDatabaseConnection()
77
    {
78
        return $GLOBALS['TYPO3_DB'];
79
    }
80
}
81