Completed
Push — master ( fd5a8f...ebf9fc )
by Oleg
04:44
created

MongodbDriver::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 3
1
<?php /** MongodbDriverMicro */
2
3
namespace Micro\Db\Drivers;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * MongoDB Driver class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Db\Drivers
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class MongodbDriver extends Driver
20
{
21
    /** @var \MongoClient $conn Connection to MongoDB */
22
    public $conn;
23
    /** @var array $collection lazy load collections */
24
    protected $collections = [];
25
    /** @var string $dbName Database name */
26
    private $dbName;
27
28
29
    /**
30
     * Construct MongoDB
31
     *
32
     * @access public
33
     *
34
     *
35
     * @param array $config
36
     * @param array $options
37
     *
38
     * @result void
39
     * @throws \MongoConnectionException
40
     * @throws \Micro\Base\Exception
41
     */
42
    public function __construct(array $config = [], array $options = [])
43
    {
44
        parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Micro\Db\Drivers\Driver as the method __construct() does only exist in the following sub-classes of Micro\Db\Drivers\Driver: Micro\Db\Drivers\MongodbDriver, Micro\Db\Drivers\MysqlDriver. 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...
45
46
        if (!empty($config['dbname'])) {
47
            $this->dbName = $config['dbname'];
48
        } else {
49
            throw new Exception('MongoDB database name not defined!');
50
        }
51
52
        try {
53
            if (!empty($config['connectionString'])) {
54
                $this->conn = new \MongoClient($config['connectionString'], $options);
55
            } else {
56
                $this->conn = new \MongoClient;
57
            }
58
        } catch (Exception $e) {
59
            if (!$config['ignoreFail']) {
60
                throw new Exception('MongoDB error connect to database');
61
            }
62
        }
63
    }
64
65
    /**
66
     * Destruct MongoDB client
67
     *
68
     * @access public
69
     * @return void
70
     */
71
    public function __destruct()
72
    {
73
        $this->conn = null;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model')
0 ignored issues
show
Unused Code introduced by
The parameter $query 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 $params 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 $fetchType 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 $fetchClass 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...
80
    {
81
        // TODO: Implement rawQuery() method.
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function listDatabases()
88
    {
89
        return $this->conn->listDBs();
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function infoDatabase($dbName)
0 ignored issues
show
Unused Code introduced by
The parameter $dbName 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...
96
    {
97
        // TODO: Implement infoDatabase() method.
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function tableExists($table)
104
    {
105
        return (bool)array_search($table, $this->listTables(), true);
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function listTables()
112
    {
113
        $this->conn->{$this->dbName}->listCollections();
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function createTable($name, array $elements = [], $params = '')
0 ignored issues
show
Unused Code introduced by
The parameter $elements 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...
120
    {
121
        $this->conn->{$this->dbName}->createCollection($name, $params);
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function clearTable($name)
128
    {
129
        $this->removeTable($name);
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function removeTable($name)
136
    {
137
        $this->conn->$name->drop();
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function fieldExists($field, $table)
0 ignored issues
show
Unused Code introduced by
The parameter $field 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 $table 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...
144
    {
145
        // TODO: Implement fieldExists() method.
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function listFields($table)
0 ignored issues
show
Unused Code introduced by
The parameter $table 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...
152
    {
153
        // TODO: Implement listFields() method.
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159
    public function fieldInfo($field, $table)
0 ignored issues
show
Unused Code introduced by
The parameter $field 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 $table 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...
160
    {
161
        // TODO: Implement fieldInfo() method.
162
    }
163
164
    /**
165
     * @inheritdoc
166
     */
167
    public function switchDatabase($dbName)
168
    {
169
        $this->conn->selectDB($dbName);
170
        $this->dbName = $dbName;
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    public function insert($table, array $line = [], $multi = false)
0 ignored issues
show
Unused Code introduced by
The parameter $table 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 $line 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 $multi 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...
177
    {
178
        // TODO: Implement insert() method.
179
    }
180
181
    /**
182
     * @inheritdoc
183
     */
184
    public function update($table, array $elements = [], $conditions = '')
0 ignored issues
show
Unused Code introduced by
The parameter $table 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 $elements 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 $conditions 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...
185
    {
186
        // TODO: Implement update() method.
187
    }
188
189
    /**
190
     * @inheritdoc
191
     */
192
    public function delete($table, $conditions, array $ph = [])
0 ignored issues
show
Unused Code introduced by
The parameter $table 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 $conditions 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 $ph 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...
193
    {
194
        // TODO: Implement delete() method.
195
    }
196
197
    /**
198
     * @inheritdoc
199
     */
200
    public function exists($table, array $params = [])
0 ignored issues
show
Unused Code introduced by
The parameter $table 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 $params 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...
201
    {
202
        // TODO: Implement exists() method.
203
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208
    public function count($subQuery = '', $table = '')
0 ignored issues
show
Unused Code introduced by
The parameter $subQuery 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 $table 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...
209
    {
210
        // TODO: Implement count() method.
211
    }
212
}
213