Passed
Branch refactor/164-removeoldcollecti... (61eb5d)
by Luke
02:41
created

TabularCollection::walk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
/**
3
 * CSVelte: Slender, elegant CSV for PHP
4
 *
5
 * Inspired by Python's CSV module and Frictionless Data and the W3C's CSV
6
 * standardization efforts, CSVelte was written in an effort to take all the
7
 * suck out of working with CSV.
8
 *
9
 * @version   v${CSVELTE_DEV_VERSION}
10
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
11
 * @author    Luke Visinoni <[email protected]>
12
 * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT)
13
 */
14
namespace CSVelte\Collection;
15
16
use BadMethodCallException;
17
use OutOfBoundsException;
18
use function CSVelte\is_traversable;
19
use function CSVelte\collect;
20
21
class TabularCollection extends MultiCollection
22
{
23
    /**
24
     * Is input data structure valid?
25
     *
26
     * In order to determine whether a given data structure is valid for a
27
     * particular collection type (tabular, numeric, etc.), we have this method.
28
     *
29
     * @param mixed $data The data structure to check
30
     * @return boolean True if data structure is tabular
31
     */
32 38
    protected function isConsistentDataStructure($data)
33
    {
34 38
        return static::isTabular($data);
35
    }
36
37
    /**
38
     * Does this collection have specified column?
39
     *
40
     * @param mixed $column The column index
41
     * @return bool
42
     */
43
    public function hasColumn($column)
44
    {
45
        try {
46 8
            $this->getColumn($column);
47 8
            return true;
48 1
        } catch (OutOfBoundsException $e) {
49 1
            return false;
50
        }
51
    }
52
53
    /**
54
     * Get column as collection
55
     *
56
     * @param mixed $column The column index
57
     * @param bool $throw Throw an exception on failure
58
     * @return AbstractCollection|false
59
     */
60
    public function getColumn($column, $throw = true)
61
    {
62 9
        $values = array_column($this->data, $column);
63 9
        if (count($values)) {
64 9
            return static::factory($values);
65
        }
66 1
        if ($throw) {
67 1
            throw new OutOfBoundsException(__CLASS__ . " could not find column: " . $column);
68
        }
69
        return false;
70
    }
71
72
    /**
73
     * Does this collection have a row at specified index?
74
     *
75
     * @param int $offset The column index
76
     * @return bool
77
     */
78
    public function hasRow($offset)
0 ignored issues
show
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        try {
81
            $this->getColumn($column);
0 ignored issues
show
Bug introduced by
The variable $column does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
82
            return true;
83
        } catch (OutOfBoundsException $e) {
84
            return false;
85
        }
86
    }
87
88
    /**
89
     * Get row at specified index.
90
     *
91
     * @param int $offset The row offset (starts from 0)
92
     * @param bool $throw Whether or not to throw an exception if row does not exist
93
     *
94
     * @return AbstractCollection|false
95
     */
96
    public function getRow($offset, $throw = true)
97
    {
98
        $index = (int) $offset;
99
        if (isset($this->data[$index])) {
100
            return static::factory($this->data[$index]);
101
        }
102
        if ($throw) {
103
            throw new OutOfBoundsException(__CLASS__ . " could not find row: " . $offset);
104
        }
105
        return false;
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 1
    public function map(callable $callback)
112
    {
113 1
        $ret = [];
114 1
        foreach ($this->data as $key => $row) {
115 1
            $ret[$key] = $callback(static::factory($row));
116 1
        }
117 1
        return static::factory($ret);
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 21
    public function walk(callable $callback, $extraContext = null)
124
    {
125 21
        foreach ($this as $offset => $row) {
126 21
            $callback(static::factory($row), $offset, $extraContext);
127 21
        }
128 21
        return $this;
129
    }
130
131
//    public function average($column)
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
132
//    {
133
//        $coll = $this->getColumnAsCollection($column);
134
//        return $coll->sum() / $coll->count();
135
//    }
136
//
137
//    public function mode($column)
138
//    {
139
//        return $this->getColumnAsCollection($column)->mode();
140
//    }
141
//
142
//    public function sum($column)
143
//    {
144
//        return $this->getColumnAsCollection($column)->sum();
145
//    }
146
//
147
//    public function median($column)
148
//    {
149
//        return $this->getColumnAsCollection($column)->median();
150
//    }
151
//
152
//    protected function getColumnAsCollection($column)
153
//    {
154
//        return static::factory(array_column($this->data, $column));
155
//    }
156
157
    /**
158
     * Magic method call
159
     *
160
     * @param string $method The name of the method
161
     * @param array $args The argument list
162
     *
163
     * @throws BadMethodCallException If no method exists
164
     *
165
     * @return mixed
166
     *
167
     * @todo Add phpdoc comments for dynamic methods
168
     * @todo throw BadMethodCallException
169
     */
170 8
    public function __call($method, $args)
171
    {
172 8
        $argc = count($args);
173 8
        if ($argc == 1 && $this->hasColumn($index = array_pop($args))) {
174 7
            $column = $this->getColumn($index);
175 7
            if (method_exists($column, $method)) {
176 7
                return call_user_func_array([$column, $method], $args);
177
            }
178
        }
179 1
        throw new BadMethodCallException("Method does not exist: " . __CLASS__ . "::{$method}()");
180
    }
181
}