UseCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/**
4
 * @file UseCommand.php
5
 * @brief This file contains the UseCommand class.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace EoC\CLI\Command;
12
13
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
19
/**
20
 * @brief Selects the specified database.
21
 * @nosubgrouping
22
 */
23
class UseCommand extends AbstractCommand {
24
25
26
  /**
27
   * @brief Configures the command.
28
   */
29
  protected function configure() {
30
    $this->setName("use");
31
    $this->setDescription("Uses the specified database");
32
    $this->setAliases(['select']);
33
34
    $this->addArgument("database",
35
      InputArgument::REQUIRED,
36
      "The CouchDB database name to use");
37
  }
38
39
40
  /**
41
   * @brief Executes the command.
42
   */
43
  protected function execute(InputInterface $input, OutputInterface $output) {
0 ignored issues
show
Coding Style introduced by
execute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
44
    $database = (string)$input->getArgument('database');
45
46 View Code Duplication
    if ($shmKey = ftok($_SERVER['PHP_SELF'], 'd')) {
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...
47
48
      if (@$shmId = shmop_open($shmKey, 'a', 0644, 0))
49
        shmop_delete($shmId);
50
51
      $shmId = shmop_open($shmKey, 'n', 0644, mb_strlen($database));
52
53
      if ($shmId) {
54
        $shmBytesWritten = shmop_write($shmId, $database, 0);
0 ignored issues
show
Unused Code introduced by
$shmBytesWritten is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
        shmop_close($shmId);
56
      }
57
      else
58
        throw new \RuntimeException("Couldn't create shared memory segment.");
59
    }
60
    else
61
      throw new \RuntimeException("Cannot get a System V IPC key.");
62
  }
63
64
}