UserArrayFromResult::next()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 5
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class to walk into a list of User objects.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 */
22
23 View Code Duplication
class UserArrayFromResult extends UserArray implements Countable {
0 ignored issues
show
Duplication introduced by
This class 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...
24
	/** @var ResultWrapper */
25
	public $res;
26
27
	/** @var int */
28
	public $key;
29
30
	/** @var bool|stdClass */
31
	public $current;
32
33
	/**
34
	 * @param ResultWrapper $res
35
	 */
36
	function __construct( $res ) {
37
		$this->res = $res;
38
		$this->key = 0;
39
		$this->setCurrent( $this->res->current() );
0 ignored issues
show
Bug introduced by
It seems like $this->res->current() can be null; however, setCurrent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
40
	}
41
42
	/**
43
	 * @param bool|stdClass $row
44
	 * @return void
45
	 */
46
	protected function setCurrent( $row ) {
47
		if ( $row === false ) {
48
			$this->current = false;
49
		} else {
50
			$this->current = User::newFromRow( $row );
0 ignored issues
show
Documentation Bug introduced by
It seems like \User::newFromRow($row) of type object<User> is incompatible with the declared type boolean|object<stdClass> of property $current.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Bug introduced by
It seems like $row defined by parameter $row on line 46 can also be of type boolean; however, User::newFromRow() does only seem to accept object<stdClass>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
51
		}
52
	}
53
54
	/**
55
	 * @return int
56
	 */
57
	public function count() {
58
		return $this->res->numRows();
59
	}
60
61
	/**
62
	 * @return User
63
	 */
64
	function current() {
65
		return $this->current;
66
	}
67
68
	function key() {
69
		return $this->key;
70
	}
71
72
	function next() {
73
		$row = $this->res->next();
74
		$this->setCurrent( $row );
75
		$this->key++;
76
	}
77
78
	function rewind() {
79
		$this->res->rewind();
80
		$this->key = 0;
81
		$this->setCurrent( $this->res->current() );
0 ignored issues
show
Bug introduced by
It seems like $this->res->current() can be null; however, setCurrent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
82
	}
83
84
	/**
85
	 * @return bool
86
	 */
87
	function valid() {
88
		return $this->current !== false;
89
	}
90
}
91