Completed
Pull Request — master (#7914)
by Blizzz
24:07 queued 05:40
created

SchemaWrapper::__call()   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 2
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
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\DB;
25
26
use Doctrine\DBAL\DBALException;
27
use Doctrine\DBAL\Schema\Schema;
28
use OCP\DB\ISchemaWrapper;
29
use OCP\IDBConnection;
30
31
class SchemaWrapper implements ISchemaWrapper {
32
33
	/** @var IDBConnection|Connection */
34
	protected $connection;
35
36
	/** @var Schema */
37
	protected $schema;
38
39
	/** @var array */
40
	protected $tablesToDelete = [];
41
42
	/**
43
	 * @param IDBConnection $connection
44
	 */
45
	public function __construct(IDBConnection $connection) {
46
		$this->connection = $connection;
47
		$this->schema = $this->connection->createSchema();
48
	}
49
50
	public function getWrappedSchema() {
51
		return $this->schema;
52
	}
53
54
	public function performDropTableCalls() {
55
		foreach ($this->tablesToDelete as $tableName => $true) {
56
			$this->connection->dropTable($tableName);
57
			unset($this->tablesToDelete[$tableName]);
58
		}
59
	}
60
61
	/**
62
	 * Gets all table names
63
	 *
64
	 * @return array
65
	 */
66
	public function getTableNamesWithoutPrefix() {
67
		$tableNames = $this->schema->getTableNames();
68
		return array_map(function($tableName) {
69
			if (strpos($tableName, $this->connection->getPrefix()) === 0) {
70
				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...
71
			}
72
73
			return $tableName;
74
		}, $tableNames);
75
	}
76
77
	// Overwritten methods
78
79
	/**
80
	 * @return array
81
	 */
82
	public function getTableNames() {
83
		return $this->schema->getTableNames();
84
	}
85
86
	/**
87
	 * @param string $tableName
88
	 *
89
	 * @return \Doctrine\DBAL\Schema\Table
90
	 * @throws \Doctrine\DBAL\Schema\SchemaException
91
	 */
92
	public function getTable($tableName) {
93
		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...
94
	}
95
96
	/**
97
	 * Does this schema have a table with the given name?
98
	 *
99
	 * @param string $tableName
100
	 *
101
	 * @return boolean
102
	 */
103
	public function hasTable($tableName) {
104
		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...
105
	}
106
107
	/**
108
	 * Creates a new table.
109
	 *
110
	 * @param string $tableName
111
	 * @return \Doctrine\DBAL\Schema\Table
112
	 */
113
	public function createTable($tableName) {
114
		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...
115
	}
116
117
	/**
118
	 * Drops a table from the schema.
119
	 *
120
	 * @param string $tableName
121
	 * @return \Doctrine\DBAL\Schema\Schema
122
	 */
123
	public function dropTable($tableName) {
124
		$this->tablesToDelete[$tableName] = true;
125
		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...
126
	}
127
128
	/**
129
	 * Gets all tables of this schema.
130
	 *
131
	 * @return \Doctrine\DBAL\Schema\Table[]
132
	 */
133
	public function getTables() {
134
		return $this->schema->getTables();
135
	}
136
}
137