Completed
Push — master ( 7f7360...1347a3 )
by Emmanuel
01:58
created

ConfigGenerateCommand::execute()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 21
cts 21
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 21
nc 4
nop 2
crap 3
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.0
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2017 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 Inet\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
    use DBTrait;
32
33
    /**
34
     * Set the command shortcut to be used in configuration
35
     *
36
     * @var string
37
     */
38
    protected $command = 'config:generate';
39
40
41
    /**
42
     * Configure the command
43
     *
44
     * @return void
45
     */
46 13
    protected function configure()
47
    {
48
        // First command : Test the DB Connexion
49 13
        $this->setName($this->command)
50 13
             ->setDescription(
51 13
                 'Generate configuration for the Anonymizer'
52 13
             )->setHelp(
53 13
                 'This command will connect to a DB and extract a list of tables / fields to a yaml file' . PHP_EOL .
54 13
                 "Usage: ./bin/anon <info>{$this->command} -u app -p app -f anon.yml</info>"
55 13
             )->addOption(
56 13
                 'host',
57 13
                 null,
58 13
                 InputOption::VALUE_REQUIRED,
59 13
                 'Host',
60 13
                 '127.0.0.1'
61 13
             )->addOption(
62 13
                 'db',
63 13
                 'd',
64 13
                 InputOption::VALUE_REQUIRED,
65 13
                 'Database Name'
66 13
             )->addOption(
67 13
                 'user',
68 13
                 'u',
69 13
                 InputOption::VALUE_REQUIRED,
70 13
                 'User Name',
71
                 get_current_user()
72 13
             )->addOption(
73 13
                 'password',
74 13
                 'p',
75 13
                 InputOption::VALUE_REQUIRED,
76 13
                 "Password (or it'll be prompted)"
77 13
             )->addOption(
78 13
                 'file',
79 13
                 'f',
80 13
                 InputOption::VALUE_REQUIRED,
81 13
                 'File',
82 13
                 'anon.yml'
83 13
             )->addOption(
84 13
                 'protect',
85 13
                 null,
86 13
                 InputOption::VALUE_NONE,
87 13
                 'Protect IDs and other fields'
88 13
             )->addOption(
89 13
                 'ignore-table',
90 13
                 null,
91 13
                 InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
92 13
                 'Table to ignore. Can be repeated'
93 13
             )->addOption(
94 13
                 'ignore-field',
95 13
                 null,
96 13
                 InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
97 13
                 'Field to ignore. Regexp in the form "table.field". Can be repeated'
98
             );
99 13
    }
100
101
    /**
102
     * Execute the command
103
     *
104
     * @param InputInterface  $input
105
     * @param OutputInterface $output
106
     *
107
     * @return void
108
     */
109 6
    protected function execute(InputInterface $input, OutputInterface $output)
110
    {
111 6
        $password = $input->getOption('password');
112 6
        if (is_null($password)) {
113 4
            $question = new Question('Password: ');
114 4
            $question->setHidden(true)->setHiddenFallback(false);
115
116 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...
117
        }
118
119 6
        $this->connectToDB(
120 6
            $input->getOption('host'),
121 6
            $input->getOption('db'),
122 6
            $input->getOption('user'),
123
            $password
124
        );
125
126 4
        $ignoreFields = $input->getOption('ignore-field');
127
128 4
        $writer = new \Inet\Neuralyzer\Configuration\Writer;
129 4
        $writer->protectCols($input->getOption('protect'));
130
131
        // Override the protection if fields are defined
132 4
        if (!empty($ignoreFields)) {
133 1
            $writer->protectCols(true);
134 1
            $writer->setProtectedCols($ignoreFields);
135
        }
136
137 4
        $writer->setIgnoredTables($input->getOption('ignore-table'));
138 4
        $data = $writer->generateConfFromDB($this->pdo, new \Inet\Neuralyzer\Guesser);
139 3
        $writer->save($data, $input->getOption('file'));
140
141 3
        $output->writeln('<comment>Configuration written to ' . $input->getOption('file') . '</comment>');
142 3
    }
143
}
144