GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RentgenExtension   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 20
Bugs 2 Features 10
Metric Value
wmc 14
c 20
b 2
f 10
lcom 1
cbo 5
dl 0
loc 134
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 78 6
A getAlias() 0 4 1
A getXsdValidationBasePath() 0 4 1
A getNamespace() 0 4 1
A setDefinition() 0 9 1
A defineParameters() 0 20 1
A isConnectionConfig() 0 6 3
1
<?php
2
3
namespace Rentgen\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Definition;
7
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
8
use Symfony\Component\DependencyInjection\Reference;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\Yaml\Yaml;
11
use Rentgen\Schema\Factory;
12
13
class RentgenExtension implements ExtensionInterface
14
{
15
    public function load(array $configs, ContainerBuilder $container)
16
    {
17
        foreach ($configs as $config) {
18
            if ($this->isConnectionConfig($config)) {
19
                $connectionConfig = $config;
20
            }
21
        }
22
        if ($container->hasParameter('connection_config')) {
23
            $connectionConfig = $container->getParameter('connection_config');
24
        }
25
26
        $this->defineParameters($container);
27
28
        $definition = new Definition('Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher');
29
        $definition->setArguments(array(new Reference('service_container')));
30
        $container->setDefinition('event_dispatcher', $definition);
31
32
        $definition = new Definition('Rentgen\Schema\Manipulation');
33
        $definition->setArguments(array(new Reference('service_container')));
34
        $container->setDefinition('rentgen.schema.manipulation', $definition);
35
36
        $definition = new Definition('Rentgen\Schema\Info');
37
        $definition->setArguments(array(new Reference('service_container')));
38
        $container->setDefinition('rentgen.schema.info', $definition);
39
40
        if (!isset($connectionConfig)) {
41
            $fileLocator = new FileLocator(getcwd());
42
            try {
43
                $configFile = $fileLocator->locate('rentgen.yml');
0 ignored issues
show
Unused Code introduced by
$configFile 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...
44
                $content = file_get_contents('rentgen.yml');
45
                $config = Yaml::parse($content);
46
                $connectionConfig = $config;
47
            } catch (\InvalidArgumentException $e) {
48
                $connectionConfig['environments']['dev'] = array(
0 ignored issues
show
Bug introduced by
The variable $connectionConfig does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
49
                    'adapter' => 'pgsql',
50
                    'host' => 'localhost',
51
                    'database' => null,
52
                    'username' => 'postgres',
53
                    'password' => '',
54
                    'port' => 5432,
55
                );
56
            }
57
        }
58
59
        $definition = new Definition('Rentgen\Database\Connection\ConnectionConfig');
60
        $definition->setArguments(array($connectionConfig['environments']));
61
        $container->setDefinition('connection_config', $definition);
62
63
        $definition = new Definition('Rentgen\Database\Connection\Connection');
64
        $definition->setArguments(array(new Reference('connection_config')));
65
        $container->setDefinition('connection', $definition);
66
67
        $this->connection = $container->getDefinition('connection');
0 ignored issues
show
Bug introduced by
The property connection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
        $this->eventDispatcher = $container->getDefinition('event_dispatcher');
0 ignored issues
show
Bug introduced by
The property eventDispatcher does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
70
        $this->setDefinition('rentgen.create_table', 'rentgen.command.manipulation.create_table.class', $container);
71
        $this->setDefinition('rentgen.drop_table', 'rentgen.command.manipulation.drop_table.class', $container);
72
        $this->setDefinition('rentgen.add_column', 'rentgen.command.manipulation.add_column.class', $container);
73
        $this->setDefinition('rentgen.drop_column', 'rentgen.command.manipulation.drop_column.class', $container);
74
        $this->setDefinition('rentgen.add_constraint', 'rentgen.command.manipulation.add_constraint.class', $container);
75
        $this->setDefinition('rentgen.drop_constraint', 'rentgen.command.manipulation.drop_constraint.class', $container);
76
        $this->setDefinition('rentgen.create_index', 'rentgen.command.manipulation.create_index.class', $container);
77
        $this->setDefinition('rentgen.create_schema', 'rentgen.command.manipulation.create_schema.class', $container);
78
        $this->setDefinition('rentgen.drop_schema', 'rentgen.command.manipulation.drop_schema.class', $container);
79
        $this->setDefinition('rentgen.drop_index', 'rentgen.command.manipulation.drop_index.class', $container);
80
81
        $this->setDefinition('rentgen.table_exists', 'rentgen.command.info.table_exists.class', $container);
82
        $this->setDefinition('rentgen.get_table', 'rentgen.command.info.get_table.class', $container);
83
        $this->setDefinition('rentgen.get_tables', 'rentgen.command.info.get_tables.class', $container);
84
        $this->setDefinition('rentgen.get_schemas', 'rentgen.command.info.get_schemas.class', $container);
85
        $this->setDefinition('rentgen.schema_exists', 'rentgen.command.info.schema_exists.class', $container);
86
87
        $definition = new Definition($container->getParameter('rentgen.command.manipulation.clear_database.class'),
88
            array(new Reference('rentgen.get_schemas')));
89
        $definition->addMethodCall('setConnection', array(new Reference('connection')));
90
        $definition->addMethodCall('setEventDispatcher', array(new Reference('event_dispatcher')));
91
        $container->setDefinition('rentgen.clear_database', $definition);
92
    }
93
94
    public function getAlias()
95
    {
96
        return 'rentgen';
97
    }
98
99
    public function getXsdValidationBasePath()
100
    {
101
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Symfony\Component\Depend...etXsdValidationBasePath of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
102
    }
103
104
    public function getNamespace()
105
    {
106
        return 'http://www.example.com/symfony/schema/';
107
    }
108
109
    private function setDefinition($name, $classParam, $container)
110
    {
111
        $className = $container->getParameter($classParam);
112
113
        $definition = new Definition($className);
114
        $definition->addMethodCall('setConnection', array(new Reference('connection')));
115
        $definition->addMethodCall('setEventDispatcher', array(new Reference('event_dispatcher')));
116
        $container->setDefinition($name, $definition);
117
    }
118
119
    private function defineParameters(ContainerBuilder $container)
120
    {
121
        $container->setParameter('rentgen.command.factory.class', 'Rentgen\Schema\Factory');
122
        $container->setParameter('rentgen.command.manipulation.create_table.class', 'Rentgen\Schema\Manipulation\CreateTableCommand');
123
        $container->setParameter('rentgen.command.manipulation.drop_table.class', 'Rentgen\Schema\Manipulation\DropTableCommand');
124
        $container->setParameter('rentgen.command.manipulation.add_column.class', 'Rentgen\Schema\Manipulation\AddColumnCommand');
125
        $container->setParameter('rentgen.command.manipulation.drop_column.class', 'Rentgen\Schema\Manipulation\DropColumnCommand');
126
        $container->setParameter('rentgen.command.manipulation.add_constraint.class', 'Rentgen\Schema\Manipulation\AddConstraintCommand');
127
        $container->setParameter('rentgen.command.manipulation.drop_constraint.class', 'Rentgen\Schema\Manipulation\DropConstraintCommand');
128
        $container->setParameter('rentgen.command.manipulation.create_index.class', 'Rentgen\Schema\Manipulation\CreateIndexCommand');
129
        $container->setParameter('rentgen.command.manipulation.drop_index.class', 'Rentgen\Schema\Manipulation\DropIndexCommand');
130
        $container->setParameter('rentgen.command.manipulation.create_schema.class', 'Rentgen\Schema\Manipulation\CreateSchemaCommand');
131
        $container->setParameter('rentgen.command.manipulation.drop_schema.class', 'Rentgen\Schema\Manipulation\DropSchemaCommand');
132
        $container->setParameter('rentgen.command.manipulation.clear_database.class', 'Rentgen\Schema\Manipulation\ClearDatabaseCommand');
133
        $container->setParameter('rentgen.command.info.table_exists.class', 'Rentgen\Schema\Info\TableExistsCommand');
134
        $container->setParameter('rentgen.command.info.get_table.class', 'Rentgen\Schema\Info\GetTableCommand');
135
        $container->setParameter('rentgen.command.info.get_tables.class', 'Rentgen\Schema\Info\GetTablesCommand');
136
        $container->setParameter('rentgen.command.info.get_schemas.class', 'Rentgen\Schema\Info\GetSchemasCommand');
137
        $container->setParameter('rentgen.command.info.schema_exists.class', 'Rentgen\Schema\Info\SchemaExistsCommand');
138
    }
139
140
    private function isConnectionConfig($config)
141
    {
142
        return isset($config['username'])
143
            && isset($config['password'])
144
            && isset($config['dsn']);
145
    }
146
}
147