InitController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 9
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onDispatch() 0 21 3
1
<?php
2
3
namespace T4web\Migrations\Controller;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use Zend\Console\Request as ConsoleRequest;
7
use Zend\Mvc\MvcEvent;
8
use Zend\Db\Adapter\Adapter as DbAdapter;
9
use Zend\Db\Sql\Ddl;
10
use Zend\Db\Sql\Sql;
11
use T4web\Migrations\Version\Version;
12
use T4web\Migrations\Exception\RuntimeException;
13
14
class InitController extends AbstractActionController
15
{
16
    /**
17
     * @var DbAdapter
18
     */
19
    private $dbAdapter;
20
21
    public function __construct(DbAdapter $dbAdapter)
22
    {
23
        $this->dbAdapter = $dbAdapter;
24
    }
25
26
    public function onDispatch(MvcEvent $e)
27
    {
28
        if (!$e->getRequest() instanceof ConsoleRequest) {
0 ignored issues
show
Bug introduced by
The method getRequest does only exist in Zend\Mvc\MvcEvent, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
29
            throw new RuntimeException('You can only use this action from a console!');
30
        }
31
32
        $table = new Ddl\CreateTable(Version::TABLE_NAME);
33
        $table->addColumn(new Ddl\Column\Integer('id', false, null, ['autoincrement' => true]));
34
        $table->addColumn(new Ddl\Column\BigInteger('version'));
35
        $table->addConstraint(new Ddl\Constraint\PrimaryKey('id'));
36
        $table->addConstraint(new Ddl\Constraint\UniqueKey('version'));
37
38
        $sql = new Sql($this->dbAdapter);
39
40
        try {
41
            $this->dbAdapter->query($sql->buildSqlString($table), DbAdapter::QUERY_MODE_EXECUTE);
42
        } catch (\Exception $e) {
43
            // currently there are no db-independent way to check if table exists
44
            // so we assume that table exists when we catch exception
45
        }
46
    }
47
}
48