Completed
Push — dev ( 3bce75...1a2bfc )
by Auke
09:55
created

pinp_selectorIterator::_rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
	require_once("mod_selector/nodes.php");
3
	require_once("mod_selector/parser.php");
4
5
	class selector {
6
		protected $nodes;
7
8
		public function __construct($string) {
9
			$parser = new selectorParser($string);
10
			$this->nodes = $parser->parse();
11
		}
12
13
		public function run($count, $offset = 0, $definitions = Array()) {
14
			if (!$this->nodes) {
15
				return false;
16
			}
17
			if ($offset >= $count) {
18
				return false;
19
			}
20
21
			$result = $this->nodes->run($count, $offset);
22
			if (isset($definitions[$result])) {
23
				$result = $definitions[$result];
24
			}
25
26
			return $result;
27
		}
28
29
		public function getIterator($count, $definitions = Array()) {
30
			return new selectorIterator($this, $count, $definitions);
31
		}
32
33
	}
34
35
	class selectorIterator implements Iterator {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
36
37
		public function __construct($selector, $count, $definitions = Array()) {
38
			$this->selector		= $selector;
0 ignored issues
show
Bug introduced by
The property selector does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
			$this->count		= $count;
0 ignored issues
show
Bug introduced by
The property count does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
			$this->offset		= 0;
0 ignored issues
show
Bug introduced by
The property offset does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
			$this->definitions	= $definitions;
0 ignored issues
show
Bug introduced by
The property definitions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
		}
43
44
		public function current() {
45
			return $this->selector->run($this->count, $this->offset, $this->definitions);
46
		}
47
48
		public function key() {
49
			return $this->offset;
50
		}
51
52
		public function next() {
53
			$this->offset = $this->offset + 1;
54
		}
55
56
		public function rewind() {
57
			$this->offset = 0;
58
		}
59
60
		public function valid() {
61
			return ($this->offset >= 0);
62
		}
63
64
	}
65
66
	class pinp_selector extends selector {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
67
68
		public function _create($string) {
69
			return new pinp_selector($string);
70
		}
71
72
		public function _run($count, $offset = 0, $definitions = Array()) {
73
			return $this->run($count, $offset, $definitions);
74
		}
75
76
		public function _getIterator($count, $definitions = Array()) {
77
			return new pinp_selectorIterator($count, $definitions);
78
		}
79
80
	}
81
82
83
	class pinp_selectorIterator extends selectorIterator {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
84
85
		public function __construct($selector, $count, $definitions = Array()) {
86
			return parent::__construct($selector, $count, $definitions);
87
		}
88
89
		public function _current() {
90
			return $this->current();
91
		}
92
93
		public function _key() {
94
			return $this->key();
95
		}
96
97
		public function _next() {
98
			return $this->next();
99
		}
100
101
		public function _rewind() {
102
			return $this->rewind();
103
		}
104
105
		public function _valid() {
106
			return $this->valid();
107
		}
108
109
	}
110