Completed
Pull Request — 3.4 (#46)
by David
22:22 queued 03:02
created

ResultIterator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 194
Duplicated Lines 6.7 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 15
c 4
b 0
f 1
lcom 1
cbo 7
dl 13
loc 194
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A executeCountQuery() 0 4 1
A toArray() 0 3 1
A map() 0 3 1
A __construct() 13 13 1
A count() 0 7 2
A getIterator() 0 11 3
A take() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A jsonSerialize() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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, \JsonSerializable {
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
	 * Retrieve an external iterator
125
	 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
126
	 * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
127
	 * <b>Traversable</b>
128
	 * @since 5.0.0
129
	 */
130
	public function getIterator()
131
	{
132
		if ($this->innerResultIterator === null) {
133
			if ($this->mode === TDBMService::MODE_CURSOR) {
134
				$this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, null, null, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery);
135
			} else {
136
				$this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, null, null, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery);
137
			}
138
		}
139
		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...
140
	}
141
142
	/**
143
	 * @param int $offset
144
	 * @return PageIterator
145
	 */
146
	public function take($offset, $limit)
147
	{
148
		return new PageIterator($this, $this->magicSql, $this->parameters, $limit, $offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode);
149
	}
150
151
	/**
152
	 * Whether a offset exists
153
	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
154
	 * @param mixed $offset <p>
155
	 * An offset to check for.
156
	 * </p>
157
	 * @return boolean true on success or false on failure.
158
	 * </p>
159
	 * <p>
160
	 * The return value will be casted to boolean if non-boolean was returned.
161
	 * @since 5.0.0
162
	 */
163
	public function offsetExists($offset)
164
	{
165
		return $this->getIterator()->offsetExists($offset);
166
	}
167
168
	/**
169
	 * Offset to retrieve
170
	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
171
	 * @param mixed $offset <p>
172
	 * The offset to retrieve.
173
	 * </p>
174
	 * @return mixed Can return all value types.
175
	 * @since 5.0.0
176
	 */
177
	public function offsetGet($offset)
178
	{
179
		return $this->getIterator()->offsetGet($offset);
180
	}
181
182
	/**
183
	 * Offset to set
184
	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
185
	 * @param mixed $offset <p>
186
	 * The offset to assign the value to.
187
	 * </p>
188
	 * @param mixed $value <p>
189
	 * The value to set.
190
	 * </p>
191
	 * @return void
192
	 * @since 5.0.0
193
	 */
194
	public function offsetSet($offset, $value)
195
	{
196
		return $this->getIterator()->offsetSet($offset, $value);
197
	}
198
199
	/**
200
	 * Offset to unset
201
	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
202
	 * @param mixed $offset <p>
203
	 * The offset to unset.
204
	 * </p>
205
	 * @return void
206
	 * @since 5.0.0
207
	 */
208
	public function offsetUnset($offset)
209
	{
210
		return $this->getIterator()->offsetUnset($offset);
211
	}
212
213
	/**
214
	 * Specify data which should be serialized to JSON
215
	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
216
	 * @return mixed data which can be serialized by <b>json_encode</b>,
217
	 * which is a value of any type other than a resource.
218
	 * @since 5.4.0
219
	 */
220
	public function jsonSerialize()
221
	{
222
		return array_map(function(AbstractTDBMObject $item) {
223
			return $item->jsonSerialize();
0 ignored issues
show
Bug introduced by
The method jsonSerialize() does not seem to exist on object<Mouf\Database\TDBM\AbstractTDBMObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
224
		}, $this->toArray());
225
	}
226
}
227