DatabaseTruncateCleanup::filterByConf()   D
last analyzed

Complexity

Conditions 9
Paths 4

Size

Total Lines 29
Code Lines 14

Duplication

Lines 15
Ratio 51.72 %

Importance

Changes 0
Metric Value
dl 15
loc 29
rs 4.909
c 0
b 0
f 0
cc 9
eloc 14
nc 4
nop 1
1
<?php namespace Tequilarapido\Cli\Commands;
2
3
use Symfony\Component\Console\Input\InputInterface;
4
use Symfony\Component\Console\Output\OutputInterface;
5
use Tequilarapido\Cli\Commands\Base\AbstractDatabaseCommand;
6
use Tequilarapido\Database\Table;
7
8
class DatabaseTruncateCleanup extends AbstractDatabaseCommand
9
{
10
11
    protected function configure()
12
    {
13
        parent::configure();
14
15
        $description = '';
16
        $description .= 'Clean up database by truncating tables specified in configuration file. ' . PHP_EOL;
17
        $description .= '  ';
18
19
        $this
20
            ->setName('db:truncate')
21
            ->setDescription($description);
22
    }
23
24 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method 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...
25
    {
26
        parent::execute($input, $output);
27
28
        // Operations config
29
        $cleanupConf = $this->config->getTruncateCleanup();
30
        if (is_null($cleanupConf)) {
31
            $this->output->warn('There is nothing to truncate according to configuration.');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method warn() does only exist in the following implementations of said interface: Tequilarapido\Cli\EnhancedOutput.

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...
32
            return;
33
        }
34
35
        // Setup connection
36
        $this->setup();
37
38
        // Tables
39
        $tables = $this->filterByConf($cleanupConf);
40
41
        // Perform
42
        $this->databaseSize['before'] = $this->db->size($this->config->getDatabaseName());
43
        $this->performTruncateOperations($tables);
44
        $this->databaseSize['after'] = $this->db->size($this->config->getDatabaseName());
45
46
        // Output gain
47
        $this->outputGain();
48
    }
49
50
    /**
51
     * @param \stdClass $cleanupConf
52
     */
53
    protected function filterByConf($cleanupConf)
54
    {
55
        $tables = array();
56
57
        // Get all tables
58
        $this->table = new Table();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Tequilarapido\Database\Table() of type object<Tequilarapido\Database\Table> is incompatible with the declared type object<Tequilarapido\Cli\Commands\Base\Table> of property $table.

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...
59
        $all = $this->table->all($this->databaseName);
60
61
        // Add simple tables
62 View Code Duplication
        if (!empty($cleanupConf->simple) && is_array($cleanupConf->simple)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
            foreach ($cleanupConf->simple as $t) {
64
                if (in_array($t, $all)) {
65
                    $tables[] = $t;
66
                }
67
            }
68
        }
69
70
        // Add multi tables
71 View Code Duplication
        if (!empty($cleanupConf->multi) && is_array($cleanupConf->multi)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
72
            foreach ($cleanupConf->multi as $t) {
73
                $matchedTables = $this->table->getTablesForMulti($t, $this->databasePrefix, $all);
74
                if (!empty($matchedTables)) {
75
                    $tables = array_merge($tables, $matchedTables);
76
                }
77
            }
78
        }
79
80
        return array_unique($tables);
81
    }
82
83
    protected function performTruncateOperations($tables)
84
    {
85
        foreach ($tables as $table) {
86
            $this->output->info("Truncating $table ...");
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method info() does only exist in the following implementations of said interface: Tequilarapido\Cli\EnhancedOutput.

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...
87
            foreach ($tables as $table) {
88
                $this->db->table($table)->truncate();
89
            }
90
        }
91
    }
92
93
}