Passed
Push — master ( c103e2...560387 )
by Emmanuel
03:39
created

ConfigGenerateCommand::execute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 5
nop 2
crap 4
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2018 Emmanuel Dyan
10
 *
11
 * @package edyan/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Edyan\Neuralyzer\Console\Commands;
19
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Question\Question;
25
26
/**
27
 * Command to generate a config file from a DB
28
 */
29
class ConfigGenerateCommand extends Command
30
{
31
    /**
32
     * Set the command shortcut to be used in configuration
33
     *
34
     * @var string
35
     */
36
    protected $command = 'config:generate';
37
38
39
    /**
40
     * Configure the command
41
     *
42
     * @return void
43
     */
44 13
    protected function configure()
45
    {
46
        // First command : Test the DB Connexion
47 13
        $this->setName($this->command)
48 13
            ->setDescription(
49 13
                'Generate configuration for the Anonymizer'
50 13
            )->setHelp(
51 13
                'This command will connect to a DB and extract a list of tables / fields to a yaml file' . PHP_EOL .
52 13
                "Usage: ./bin/neuralyzer <info>{$this->command} -u app -p app -f anon.yml</info>"
53 13
            )->addOption(
54 13
                'driver',
55 13
                null,
56 13
                InputOption::VALUE_REQUIRED,
57 13
                'Driver (check Doctrine documentation to have the list)',
58 13
                'pdo_mysql'
59 13
            )->addOption(
60 13
                'host',
61 13
                null,
62 13
                InputOption::VALUE_REQUIRED,
63 13
                'Host',
64 13
                '127.0.0.1'
65 13
            )->addOption(
66 13
                'db',
67 13
                'd',
68 13
                InputOption::VALUE_REQUIRED,
69 13
                'Database Name'
70 13
            )->addOption(
71 13
                'user',
72 13
                'u',
73 13
                InputOption::VALUE_REQUIRED,
74 13
                'User Name',
75 13
                get_current_user()
76 13
            )->addOption(
77 13
                'password',
78 13
                'p',
79 13
                InputOption::VALUE_REQUIRED,
80 13
                "Password (or it'll be prompted)"
81 13
            )->addOption(
82 13
                'file',
83 13
                'f',
84 13
                InputOption::VALUE_REQUIRED,
85 13
                'File',
86 13
                'anon.yml'
87 13
            )->addOption(
88 13
                'protect',
89 13
                null,
90 13
                InputOption::VALUE_NONE,
91 13
                'Protect IDs and other fields'
92 13
            )->addOption(
93 13
                'ignore-table',
94 13
                null,
95 13
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
96 13
                'Table to ignore. Can be repeated'
97 13
            )->addOption(
98 13
                'ignore-field',
99 13
                null,
100 13
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
101 13
                'Field to ignore. Regexp in the form "table.field". Can be repeated'
102
            );
103 13
    }
104
105
    /**
106
     * Execute the command
107
     *
108
     * @param InputInterface  $input
109
     * @param OutputInterface $output
110
     *
111
     * @return void
112
     */
113 6
    protected function execute(InputInterface $input, OutputInterface $output)
114
    {
115
        // Throw an exception immediately if we dont have the required DB parameter
116 6
        if (empty($input->getOption('db'))) {
117 1
            throw new \InvalidArgumentException('Database name is required (--db)');
118
        }
119
120 5
        $password = $input->getOption('password');
121 5
        if (is_null($password)) {
122 4
            $question = new Question('Password: ');
123 4
            $question->setHidden(true)->setHiddenFallback(false);
124
125 4
            $password = $this->getHelper('question')->ask($input, $output, $question);
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 ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

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...
126
        }
127
128 5
        $ignoreFields = $input->getOption('ignore-field');
129
130
        // Now work on the DB
131 5
        $db = new \Edyan\Neuralyzer\Anonymizer\DB([
132 5
            'driver' => $input->getOption('driver'),
133 5
            'host' => $input->getOption('host'),
134 5
            'dbname' => $input->getOption('db'),
135 5
            'user' => $input->getOption('user'),
136 5
            'password' => $password,
137
        ]);
138
139 5
        $writer = new \Edyan\Neuralyzer\Configuration\Writer;
140 5
        $writer->protectCols($input->getOption('protect'));
141
142
        // Override the protection if fields are defined
143 5
        if (!empty($ignoreFields)) {
144 1
            $writer->protectCols(true);
145 1
            $writer->setProtectedCols($ignoreFields);
146
        }
147
148 5
        $writer->setIgnoredTables($input->getOption('ignore-table'));
149 5
        $data = $writer->generateConfFromDB($db, new \Edyan\Neuralyzer\Guesser);
150 3
        $writer->save($data, $input->getOption('file'));
151
152 3
        $output->writeln('<comment>Configuration written to ' . $input->getOption('file') . '</comment>');
153 3
    }
154
}
155