Completed
Push — master ( ca5656...60398b )
by Morris
32:43 queued 16:28
created

SchemaWrapper::getTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\DB;
23
24
use Doctrine\DBAL\DBALException;
25
use Doctrine\DBAL\Schema\Schema;
26
use OCP\IDBConnection;
27
28
class SchemaWrapper {
29
30
	/** @var IDBConnection|Connection */
31
	protected $connection;
32
33
	/** @var Schema */
34
	protected $schema;
35
36
	/** @var array */
37
	protected $tablesToDelete;
38
39
	/**
40
	 * @param IDBConnection $connection
41
	 */
42
	public function __construct(IDBConnection $connection) {
43
		$this->connection = $connection;
44
		$this->schema = $this->connection->createSchema();
45
	}
46
47
	public function getWrappedSchema() {
48
		return $this->schema;
49
	}
50
51
	public function performDropTableCalls() {
52
		foreach ($this->tablesToDelete as $tableName => $true) {
53
			$this->connection->dropTable($tableName);
54
			unset($this->tablesToDelete[$tableName]);
55
		}
56
	}
57
58
	/**
59
	 * Gets all table names
60
	 *
61
	 * @return array
62
	 */
63
	public function getTableNamesWithoutPrefix() {
64
		$tableNames = $this->schema->getTableNames();
65
		return array_map(function($tableName) {
66
			if (strpos($tableName, $this->connection->getPrefix()) === 0) {
67
				return substr($tableName, strlen($this->connection->getPrefix()));
0 ignored issues
show
Bug introduced by
The method getPrefix does only exist in OC\DB\Connection, but not in OCP\IDBConnection.

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...
68
			}
69
70
			return $tableName;
71
		}, $tableNames);
72
	}
73
74
	// Overwritten methods
75
76
	/**
77
	 * @param string $tableName
78
	 *
79
	 * @return \Doctrine\DBAL\Schema\Table
80
	 * @throws \Doctrine\DBAL\Schema\SchemaException
81
	 */
82
	public function getTable($tableName) {
83
		return $this->schema->getTable($this->connection->getPrefix() . $tableName);
0 ignored issues
show
Bug introduced by
The method getPrefix does only exist in OC\DB\Connection, but not in OCP\IDBConnection.

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...
84
	}
85
86
	/**
87
	 * Does this schema have a table with the given name?
88
	 *
89
	 * @param string $tableName
90
	 *
91
	 * @return boolean
92
	 */
93
	public function hasTable($tableName) {
94
		return $this->schema->hasTable($this->connection->getPrefix() . $tableName);
0 ignored issues
show
Bug introduced by
The method getPrefix does only exist in OC\DB\Connection, but not in OCP\IDBConnection.

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...
95
	}
96
97
	/**
98
	 * Creates a new table.
99
	 *
100
	 * @param string $tableName
101
	 * @return \Doctrine\DBAL\Schema\Table
102
	 */
103
	public function createTable($tableName) {
104
		return $this->schema->createTable($this->connection->getPrefix() . $tableName);
0 ignored issues
show
Bug introduced by
The method getPrefix does only exist in OC\DB\Connection, but not in OCP\IDBConnection.

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...
105
	}
106
107
	/**
108
	 * Renames a table.
109
	 *
110
	 * @param string $oldTableName
111
	 * @param string $newTableName
112
	 *
113
	 * @return \Doctrine\DBAL\Schema\Schema
114
	 * @throws DBALException
115
	 */
116
	public function renameTable($oldTableName, $newTableName) {
0 ignored issues
show
Unused Code introduced by
The parameter $oldTableName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $newTableName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
		throw new DBALException('Renaming tables is not supported. Please create and drop the tables manually.');
118
	}
119
120
	/**
121
	 * Drops a table from the schema.
122
	 *
123
	 * @param string $tableName
124
	 * @return \Doctrine\DBAL\Schema\Schema
125
	 */
126
	public function dropTable($tableName) {
127
		$this->tablesToDelete[$tableName] = true;
128
		return $this->schema->dropTable($this->connection->getPrefix() . $tableName);
0 ignored issues
show
Bug introduced by
The method getPrefix does only exist in OC\DB\Connection, but not in OCP\IDBConnection.

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...
129
	}
130
131
	/**
132
	 * @param string $name
133
	 * @param array $arguments
134
	 * @return mixed
135
	 */
136
	public function __call($name, $arguments) {
137
		return call_user_func_array([$this->schema, $name], $arguments);
138
	}
139
}
140