Completed
Pull Request — 3.4 (#46)
by David
07:34 queued 01:29
created

ResultIterator::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 2
nc 1
nop 1
1
<?php
2
namespace Mouf\Database\TDBM;
3
4
use Doctrine\DBAL\Driver\Connection;
5
use Doctrine\DBAL\Statement;
6
use Mouf\Database\MagicQuery;
7
use Porpaginas\Result;
8
use Traversable;
9
10
/*
11
 Copyright (C) 2006-2015 David Négrier - THE CODING MACHINE
12
13
 This program is free software; you can redistribute it and/or modify
14
 it under the terms of the GNU General Public License as published by
15
 the Free Software Foundation; either version 2 of the License, or
16
 (at your option) any later version.
17
18
 This program is distributed in the hope that it will be useful,
19
 but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 GNU General Public License for more details.
22
23
 You should have received a copy of the GNU General Public License
24
 along with this program; if not, write to the Free Software
25
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26
 */
27
28
29
/**
30
 * Iterator used to retrieve results.
31
 *
32
 */
33
class ResultIterator implements Result, \ArrayAccess {
34
35
	/**
36
	 *
37
	 * @var Statement
38
	 */
39
	protected $statement;
40
	
41
	protected $fetchStarted = false;
42
	private $objectStorage;
43
	private $className;
44
45
46
	private $tdbmService;
47
	private $magicSql;
48
	private $magicSqlCount;
49
	private $parameters;
50
	private $columnDescriptors;
51
	private $magicQuery;
52
53
	/**
54
	 * @var InnerResultIterator
55
	 */
56
	private $innerResultIterator;
57
58
	/**
59
	 * The key of the current retrieved object.
60
	 *
61
	 * @var int
62
	 */
63
	protected $key = -1;
64
65
	protected $current = null;
66
67
	private $databasePlatform;
68
69
	private $totalCount;
70
71
	private $mode;
72
	
73 View Code Duplication
	public function __construct($magicSql, $magicSqlCount, array $parameters, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode)
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...
74
	{
75
		$this->magicSql = $magicSql;
76
		$this->magicSqlCount = $magicSqlCount;
77
		$this->objectStorage = $objectStorage;
78
		$this->className = $className;
79
		$this->tdbmService = $tdbmService;
80
		$this->parameters = $parameters;
81
		$this->columnDescriptors = $columnDescriptors;
82
		$this->magicQuery = $magicQuery;
83
		$this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform();
84
		$this->mode = $mode;
85
	}
86
87
	protected function executeCountQuery() {
88
		$sql = $this->magicQuery->build($this->magicSqlCount, $this->parameters);
89
		$this->totalCount = $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters);
90
	}
91
92
	/**
93
	 * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings)
94
	 * @return int
95
	 */
96
	public function count()
97
	{
98
		if ($this->totalCount === null) {
99
			$this->executeCountQuery();
100
		}
101
		return $this->totalCount;
102
	}
103
104
	/**
105
	 * Casts the result set to a PHP array.
106
	 *
107
	 * @return array
108
	 */
109
	public function toArray() {
110
		return iterator_to_array($this->getIterator());
111
	}
112
113
	/**
114
	 * Returns a new iterator mapping any call using the $callable function.
115
	 *
116
	 * @param callable $callable
117
	 * @return MapIterator
118
	 */
119
	public function map(callable $callable) {
120
		return new MapIterator($this->getIterator(), $callable);
121
	}
122
123
	/**
124
	 * Fetches all records (this could impact into your site performance) and rewinds the cursor
125
	 * @param boolean $asRecords Bind into record class?
126
	 * @return array[Record_PDO]|array[array] Array of records or arrays (depends on $asRecords)
127
	 */
128
	/*public function getAll($asRecords = true)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
129
	{
130
		$all = array();
131
		$this->rewind();
132
		foreach ($this->pdoStatement as $id => $doc) {
133
			if ($asRecords)
134
				$all[$id] = $this->cast($doc);
135
			else
136
				$all[$id] = $doc;
137
		}
138
		return $all;
139
	}*/
140
	/**
141
	 * @return PDOStatement
142
	 */
143
	/*public function getPDOStatement()
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
144
	{
145
		return $this->pdoStatement;
146
	}*/
147
148
	/**
149
	 * Retrieve an external iterator
150
	 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
151
	 * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
152
	 * <b>Traversable</b>
153
	 * @since 5.0.0
154
	 */
155
	public function getIterator()
156
	{
157
		if ($this->innerResultIterator === null) {
158
			if ($this->mode === TDBMService::MODE_CURSOR) {
159
				$this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, null, null, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery);
160
			} else {
161
				$this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, null, null, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery);
162
			}
163
		}
164
		return $this->innerResultIterator;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->innerResultIterator; (Mouf\Database\TDBM\InnerResultIterator) is incompatible with the return type declared by the interface Porpaginas\Result::getIterator of type Porpaginas\Iterator.

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...
165
	}
166
167
	/**
168
	 * @param int $offset
169
	 * @return PageIterator
170
	 */
171
	public function take($offset, $limit)
172
	{
173
		return new PageIterator($this, $this->magicSql, $this->parameters, $limit, $offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode);
174
	}
175
176
	/**
177
	 * Whether a offset exists
178
	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
179
	 * @param mixed $offset <p>
180
	 * An offset to check for.
181
	 * </p>
182
	 * @return boolean true on success or false on failure.
183
	 * </p>
184
	 * <p>
185
	 * The return value will be casted to boolean if non-boolean was returned.
186
	 * @since 5.0.0
187
	 */
188
	public function offsetExists($offset)
189
	{
190
		return $this->getIterator()->offsetExists($offset);
191
	}
192
193
	/**
194
	 * Offset to retrieve
195
	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
196
	 * @param mixed $offset <p>
197
	 * The offset to retrieve.
198
	 * </p>
199
	 * @return mixed Can return all value types.
200
	 * @since 5.0.0
201
	 */
202
	public function offsetGet($offset)
203
	{
204
		return $this->getIterator()->offsetGet($offset);
205
	}
206
207
	/**
208
	 * Offset to set
209
	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
210
	 * @param mixed $offset <p>
211
	 * The offset to assign the value to.
212
	 * </p>
213
	 * @param mixed $value <p>
214
	 * The value to set.
215
	 * </p>
216
	 * @return void
217
	 * @since 5.0.0
218
	 */
219
	public function offsetSet($offset, $value)
220
	{
221
		return $this->getIterator()->offsetSet($offset, $value);
222
	}
223
224
	/**
225
	 * Offset to unset
226
	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
227
	 * @param mixed $offset <p>
228
	 * The offset to unset.
229
	 * </p>
230
	 * @return void
231
	 * @since 5.0.0
232
	 */
233
	public function offsetUnset($offset)
234
	{
235
		return $this->getIterator()->offsetUnset($offset);
236
	}
237
}
238