Completed
Push — master ( 600ac0...da8e51 )
by Arjay
03:51
created

OracleBuilder::drop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\Oci8\Schema;
4
5
use Closure;
6
use Illuminate\Database\Connection;
7
use Illuminate\Database\Schema\Builder;
8
9
class OracleBuilder extends Builder
10
{
11
    /**
12
     * @var \Yajra\Oci8\Schema\OracleAutoIncrementHelper
13
     */
14
    public $helper;
15
16
    /**
17
     * @var \Yajra\Oci8\Schema\Comment
18
     */
19
    public $comment;
20
21
    /**
22
     * @param Connection $connection
23
     */
24
    public function __construct(Connection $connection)
25
    {
26
        $this->connection = $connection;
27
        $this->grammar    = $connection->getSchemaGrammar();
28
        $this->helper     = new OracleAutoIncrementHelper($connection);
29
        $this->comment    = new Comment($connection);
30
    }
31
32
    /**
33
     * Create a new table on the schema.
34
     *
35
     * @param  string $table
36
     * @param  Closure $callback
37
     * @return \Illuminate\Database\Schema\Blueprint
38
     */
39 View Code Duplication
    public function create($table, Closure $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
40
    {
41
        $blueprint = $this->createBlueprint($table);
42
43
        $blueprint->create();
44
45
        $callback($blueprint);
46
47
        $this->build($blueprint);
48
49
        $this->comment->setComments($blueprint);
50
51
        $this->helper->createAutoIncrementObjects($blueprint, $table);
52
    }
53
54
    /**
55
     * Changes an existing table on the schema.
56
     *
57
     * @param  string $table
58
     * @param  Closure $callback
59
     * @return \Illuminate\Database\Schema\Blueprint
60
     */
61 View Code Duplication
    public function table($table, Closure $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
    {
63
        $blueprint = $this->createBlueprint($table);
64
65
        $callback($blueprint);
66
67
        foreach ($blueprint->getCommands() as $command) {
68
            if ($command->get('name') == 'drop') {
69
                $this->helper->dropAutoIncrementObjects($table);
70
            }
71
        }
72
73
        $this->build($blueprint);
74
75
        $this->comment->setComments($blueprint);
76
    }
77
78
    /**
79
     * Create a new command set with a Closure.
80
     *
81
     * @param  string $table
82
     * @param  Closure $callback
83
     * @return \Illuminate\Database\Schema\Blueprint
84
     */
85
    protected function createBlueprint($table, Closure $callback = null)
86
    {
87
        $blueprint = new OracleBlueprint($table, $callback);
88
        $blueprint->setTablePrefix($this->connection->getTablePrefix());
89
90
        return $blueprint;
91
    }
92
93
    /**
94
     * Drop a table from the schema.
95
     *
96
     * @param  string $table
97
     * @return \Illuminate\Database\Schema\Blueprint
98
     */
99
    public function drop($table)
100
    {
101
        $this->helper->dropAutoIncrementObjects($table);
102
        parent::drop($table);
103
    }
104
105
    /**
106
     * Indicate that the table should be dropped if it exists.
107
     *
108
     * @return \Illuminate\Support\Fluent
109
     */
110
    public function dropIfExists($table)
111
    {
112
        $this->helper->dropAutoIncrementObjects($table);
113
        parent::dropIfExists($table);
114
    }
115
116
    /**
117
     * Determine if the given table exists.
118
     *
119
     * @param  string $table
120
     * @return bool
121
     */
122
    public function hasTable($table)
123
    {
124
        $sql = $this->grammar->compileTableExists();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Illuminate\Database\Schema\Grammars\Grammar as the method compileTableExists() does only exist in the following sub-classes of Illuminate\Database\Schema\Grammars\Grammar: Illuminate\Database\Schema\Grammars\MySqlGrammar, Illuminate\Database\Sche...rammars\PostgresGrammar, Illuminate\Database\Schema\Grammars\SQLiteGrammar, Illuminate\Database\Sche...ammars\SqlServerGrammar, Yajra\Oci8\Schema\Grammars\OracleGrammar. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
125
126
        $database = $this->connection->getConfig('username');
127
        $table    = $this->connection->getTablePrefix() . $table;
128
129
        return count($this->connection->select($sql, [$database, $table])) > 0;
130
    }
131
132
    /**
133
     * Get the column listing for a given table.
134
     *
135
     * @param  string $table
136
     * @return array
137
     */
138
    public function getColumnListing($table)
139
    {
140
        $database = $this->connection->getConfig('username');
141
        $table    = $this->connection->getTablePrefix() . $table;
142
143
        $results = $this->connection->select($this->grammar->compileColumnExists($database, $table));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Illuminate\Database\Schema\Grammars\Grammar as the method compileColumnExists() does only exist in the following sub-classes of Illuminate\Database\Schema\Grammars\Grammar: Illuminate\Database\Schema\Grammars\MySqlGrammar, Illuminate\Database\Sche...rammars\PostgresGrammar, Illuminate\Database\Schema\Grammars\SQLiteGrammar, Illuminate\Database\Sche...ammars\SqlServerGrammar, Yajra\Oci8\Schema\Grammars\OracleGrammar. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
144
145
        return $this->connection->getPostProcessor()->processColumnListing($results);
146
    }
147
}
148