ComposingIterator::__call()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
cc 5
nc 5
nop 2
1
<?php
2
3
namespace itertools;
4
5
use BadMethodCallException;
6
use EmptyIterator;
7
use ReflectionClass;
8
9
10
class ComposingIterator extends ReferencingIterator
11
{
12
	protected static $externalFilters = array(
13
	);
14
15
	protected static $externalSources = array(
16
		'glob' => true,
17
	);
18
19
	protected static $filters = array(
20
		'caching' => true,
21
		'callbackFilter' => true,
22
		'chain' => true,
23
		'chunking' => true,
24
		'currentCached' => true,
25
		'dropWhile' => true,
26
		'groupBy' => true,
27
		'history' => true,
28
		'innerExposing' => true,
29
		'lookAhead' => true,
30
		'map' => true,
31
		'slice' => true,
32
		'stopwatch' => true,
33
		'stringCsv' => true,
34
		'takeWhile' => true,
35
		'unique' => true,
36
	);
37
38
	protected static $sources = array(
39
		'fileCsv' => true,
40
		'fileLine' => true,
41
		'pdo' => true,
42
		'processCsv' => true,
43
		'range' => true,
44
		'referencing' => true,
45
		'repeat' => true,
46
		'zip' => true,
47
	);
48
49
	const USE_KEYS = true;
50
	const DONT_USE_KEYS = false;
51
52
	protected $innerIterator;
53
54
	public function __construct()
55
	{
56
		parent::__construct(new EmptyIterator());
57
	}
58
59
	public static function newInstance()
60
	{
61
		return new self();
62
	}
63
64
	public function toArray($useKeys = self::USE_KEYS)
65
	{
66
		return iterator_to_array($this, $useKeys);
67
	}
68
69
	public function count()
70
	{
71
		return iterator_count($this);
72
	}
73
74
	public function filter($callback)
75
	{
76
		return $this->callbackFilter($callback);
0 ignored issues
show
Bug introduced by
The method callbackFilter() does not exist on itertools\ComposingIterator. Did you maybe mean filter()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
	}
78
79
	public function __call($name, $arguments)
80
	{
81
		switch(true) {
82
			case array_key_exists($name, self::$externalFilters):
83
				return $this->pushIteratorByClassName(ucfirst($name) . 'Iterator', array_merge(array($this->getInnerIterator()), $arguments));
84
85
			case array_key_exists($name, self::$filters):
86
				return $this->pushIteratorByClassName(__NAMESPACE__ . '\\' . ucfirst($name) . 'Iterator', array_merge(array($this->getInnerIterator()), $arguments));
87
88
			case array_key_exists($name, self::$externalSources):
89
				return $this->pushIteratorByClassName(ucfirst($name) . 'Iterator', $arguments);
90
91
			case array_key_exists($name, self::$sources):
92
				return $this->pushIteratorByClassName(__NAMESPACE__ . '\\' . ucfirst($name) . 'Iterator', $arguments);
93
94
			default:
95
				throw new BadMethodCallException('Call to unknown method: ' . __NAMESPACE__ . '\\' . __CLASS__ . '::' . $name);
96
		}
97
	}
98
99
	protected function pushIteratorByClassName($name, $arguments)
100
	{
101
		$reflector = new ReflectionClass($name);
102
		return $this->setInnerIterator($reflector->newInstanceArgs($arguments));
103
	}
104
105
	public function chunk($chunkSize)
106
	{
107
		return $this->chunking($chunkSize);
0 ignored issues
show
Documentation Bug introduced by
The method chunking does not exist on object<itertools\ComposingIterator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
108
	}
109
110
	public function zipWith($iterable)
111
	{
112
		return $this->setInnerIterator(new ZipIterator(array($this->getInnerIterator(), $iterable)));
113
	}
114
115
	public function zipWithAll(array $iterables)
116
	{
117
		return $this->setInnerIterator(new ZipIterator(array_merge(array($this->getInnerIterator()), $iterables)));
118
	}
119
120
	public function fixedLengthFormattedStringFromTemplate($template, array $nameMap = array(), array $options = array())
121
	{
122
		return $this->setInnerIterator(FixedLengthFormattedStringIterator::newFromTemplate($this->getInnerIterator(), $template, $nameMap, $options));
123
	}
124
125
	public function skipFirst()
126
	{
127
		return $this->setInnerIterator(new SliceIterator($this->getInnerIterator(), 1));
128
	}
129
130
	public function cacheCurrent()
131
	{
132
		return $this->currentCached();
0 ignored issues
show
Documentation Bug introduced by
The method currentCached does not exist on object<itertools\ComposingIterator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
133
	}
134
135
	public function invoke($method)
136
	{
137
		return $this->map(function($object) use ($method) { return $object->$method(); });
0 ignored issues
show
Documentation Bug introduced by
The method map does not exist on object<itertools\ComposingIterator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
138
	}
139
140
	public function source($iterable)
141
	{
142
		return $this->setInnerIterator($iterable);
143
	}
144
145
    public function skipEmptyRows()
146
    {
147
        return $this->filter(function($row) {
148
            if(is_array($row)) {
149
                return "" != trim(implode("", $row));
150
            }
151
152
            return "" != $row;
153
        });
154
    }
155
}
156
 
157