Completed
Push — master ( f5a8cf...dc6729 )
by Christian
07:18
created

AbstractDatabaseCommand::getDatabaseHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Database;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
use N98\Magento\Command\Database\Compressor\AbstractCompressor;
7
use N98\Magento\Command\Database\Compressor\Compressor;
8
use N98\Magento\DbSettings;
9
use N98\Util\Console\Helper\DatabaseHelper;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
abstract class AbstractDatabaseCommand extends AbstractMagentoCommand
13
{
14
    /**
15
     * @var array|DbSettings
16
     */
17
    protected $dbSettings;
18
19
    /**
20
     * @var bool
21
     */
22
    protected $isSocketConnect = false;
23
24
    /**
25
     * @param OutputInterface $output
26
     */
27
    protected function detectDbSettings(OutputInterface $output)
28
    {
29
        $database = $database = $this->getDatabaseHelper();
30
        $this->dbSettings = $database->getDbSettings($output);
31
    }
32
33
    /**
34
     * @param $name
35
     *
36
     * @return mixed
37
     */
38
    public function __get($name)
39
    {
40
        if ($name == '_connection') {
41
            // TODO(tk): deprecate
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
            return $this->getDatabaseHelper()->getConnection();
43
        }
44
    }
45
46
    /**
47
     * Generate help for compression
48
     *
49
     * @return string
50
     */
51
    protected function getCompressionHelp()
52
    {
53
        $messages = array();
54
        $messages[] = '';
55
        $messages[] = '<comment>Compression option</comment>';
56
        $messages[] = ' Supported compression: gzip';
57
        $messages[] = ' The gzip cli tool has to be installed.';
58
        $messages[] = ' Additionally, for data-to-csv option tar cli tool has to be installed too.';
59
60
        return implode(PHP_EOL, $messages);
61
    }
62
63
    /**
64
     * @param string $type
65
     * @return Compressor
66
     * @deprecated Since 1.97.29; use AbstractCompressor::create() instead
67
     */
68
    protected function getCompressor($type)
69
    {
70
        return AbstractCompressor::create($type);
71
    }
72
73
    /**
74
     * @return string
75
     *
76
     * @deprecated Please use database helper
77
     */
78
    protected function getMysqlClientToolConnectionString()
79
    {
80
        return $this->getDatabaseHelper()->getMysqlClientToolConnectionString();
81
    }
82
83
    /**
84
     * Creates a PDO DSN for the adapter from $this->_config settings.
85
     *
86
     * @see Zend_Db_Adapter_Pdo_Abstract
87
     * @return string
88
     *
89
     * @deprecated Please use database helper
90
     */
91
    protected function _dsn()
92
    {
93
        return $this->getDatabaseHelper()->dsn();
94
    }
95
96
    /**
97
     * @return DatabaseHelper
98
     */
99
    protected function getDatabaseHelper()
100
    {
101
        return $this->getHelper('database');
102
    }
103
104
    /**
105
     * @param array $excludes
106
     * @param array $definitions
107
     * @param array $resolved Which definitions where already resolved -> prevent endless loops
108
     *
109
     * @return array
110
     *
111
     * @deprecated Please use database helper
112
     */
113
    protected function resolveTables(array $excludes, array $definitions, array $resolved = array())
114
    {
115
        return $this->getHelper('database')->resolveTables($excludes, $definitions, $resolved);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method resolveTables() does only exist in the following implementations of said interface: N98\Util\Console\Helper\DatabaseHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
116
    }
117
}
118