Completed
Push — master ( b582f9...2d195d )
by Ori
01:37
created

Table::__construct()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 12
nop 3
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\tableschema;
4
5
use frictionlessdata\tableschema\DataSources\CsvDataSource;
6
use frictionlessdata\tableschema\Exceptions\DataSourceException;
7
8
/**
9
 * represents a data source which validates against a table schema
10
 * provides interfaces for validating the data and iterating over it
11
 * casts all values to their native values according to the table schema.
12
 */
13
class Table implements \Iterator
14
{
15
    public $csvDialect;
16
17
    /**
18
     * @param DataSources\DataSourceInterface $dataSource
19
     * @param Schema                          $schema
20
     * @param object                          $csvDialect
21
     *
22
     * @throws Exceptions\DataSourceException
23
     */
24
    public function __construct($dataSource, $schema = null, $csvDialect = null)
25
    {
26
        $this->csvDialect = new CsvDialect($csvDialect);
27
        if (!is_a($dataSource, 'frictionlessdata\\tableschema\\DataSources\\BaseDataSource')) {
28
            // TODO: more advanced data source detection
29
            $dataSource = new CsvDataSource($dataSource);
30
        }
31
        if (is_a($dataSource, 'frictionlessdata\\tableschema\\DataSources\\CsvDataSource')) {
32
            $dataSource->setCsvDialect($this->csvDialect);
33
        }
34
        $this->dataSource = $dataSource;
35
        if (!is_a($schema, 'frictionlessdata\\tableschema\\Schema')) {
36
            if ($schema) {
37
                $schema = new Schema($schema);
38
            } else {
39
                $schema = new InferSchema();
40
            }
41
        }
42
        $this->schema = $schema;
43
        $this->dataSource->open();
44
        $this->uniqueFieldValues = [];
45
    }
46
47
    /**
48
     * @param DataSources\DataSourceInterface $dataSource
49
     * @param Schema                          $schema
50
     * @param int                             $numPeekRows
51
     *
52
     * @return array of validation errors
53
     */
54
    public static function validate($dataSource, $schema, $numPeekRows = 10, $csvDialect = null)
55
    {
56
        try {
57
            $table = new static($dataSource, $schema, $csvDialect);
58
        } catch (Exceptions\DataSourceException $e) {
59
            return [new SchemaValidationError(SchemaValidationError::LOAD_FAILED, $e->getMessage())];
60
        }
61
        if ($numPeekRows > 0) {
62
            $i = 0;
63
            try {
64
                foreach ($table as $row) {
65
                    if (++$i > $numPeekRows) {
66
                        break;
67
                    }
68
                }
69
            } catch (Exceptions\DataSourceException $e) {
0 ignored issues
show
Unused Code introduced by
catch (\frictionlessdata... $e->getMessage()))); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70
                // general error in getting the next row from the data source
71
                return [new SchemaValidationError(SchemaValidationError::ROW_VALIDATION, [
72
                    'row' => $i,
73
                    'error' => $e->getMessage(),
74
                ])];
75
            } catch (Exceptions\FieldValidationException $e) {
76
                // validation error in one of the fields
77
                return array_map(function ($validationError) use ($i) {
78
                    return new SchemaValidationError(SchemaValidationError::ROW_FIELD_VALIDATION, [
79
                        'row' => $i + 1,
80
                        'field' => $validationError->extraDetails['field'],
81
                        'error' => $validationError->extraDetails['error'],
82
                        'value' => $validationError->extraDetails['value'],
83
                    ]);
84
                }, $e->validationErrors);
85
            }
86
        }
87
88
        return [];
89
    }
90
91
    public function schema($numPeekRows = 10)
92
    {
93
        $this->ensureInferredSchema($numPeekRows);
94
95
        return $this->schema;
96
    }
97
98
    public function headers($numPeekRows = 10)
99
    {
100
        $this->ensureInferredSchema($numPeekRows);
101
102
        return array_keys($this->schema->fields());
103
    }
104
105
    public function read()
106
    {
107
        $rows = [];
108
        foreach ($this as $row) {
109
            $rows[] = $row;
110
        }
111
112
        return $rows;
113
    }
114
115
    public function save($outputDataSource)
116
    {
117
        return $this->dataSource->save($outputDataSource);
118
    }
119
120
    /**
121
     * called on each iteration to get the next row
122
     * does validation and casting on the row.
123
     *
124
     * @return mixed[]
125
     *
126
     * @throws Exceptions\FieldValidationException
127
     * @throws Exceptions\DataSourceException
128
     */
129
    public function current()
130
    {
131
        if (count($this->castRows) > 0) {
132
            $row = array_shift($this->castRows);
133
        } else {
134
            $row = $this->schema->castRow($this->dataSource->getNextLine());
135
            foreach ($this->schema->fields() as $field) {
136
                if ($field->unique()) {
137
                    if (!array_key_exists($field->name(), $this->uniqueFieldValues)) {
138
                        $this->uniqueFieldValues[$field->name()] = [];
139
                    }
140
                    $value = $row[$field->name()];
141
                    if (in_array($value, $this->uniqueFieldValues[$field->name()])) {
142
                        throw new DataSourceException('field must be unique', $this->currentLine);
143
                    } else {
144
                        $this->uniqueFieldValues[$field->name()][] = $value;
145
                    }
146
                }
147
            }
148
        }
149
150
        return $row;
151
    }
152
153
    // not interesting, standard iterator functions
154
    // to simplify we prevent rewinding - so you can only iterate once
155
    public function __destruct()
156
    {
157
        $this->dataSource->close();
158
    }
159
160
    public function rewind()
161
    {
162
        if ($this->currentLine == 0) {
163
            $this->currentLine = 1;
164
        } elseif (count($this->castRows) == 0) {
165
            $this->currentLine = 1;
166
            $this->dataSource->open();
167
        }
168
    }
169
170
    public function key()
171
    {
172
        return $this->currentLine - count($this->castRows);
173
    }
174
175
    public function next()
176
    {
177
        if (count($this->castRows) == 0) {
178
            ++$this->currentLine;
179
        }
180
    }
181
182
    public function valid()
183
    {
184
        return count($this->castRows) > 0 || !$this->dataSource->isEof();
185
    }
186
187
    protected $currentLine = 0;
188
    protected $dataSource;
189
    protected $schema;
190
    protected $uniqueFieldValues;
191
    protected $castRows = [];
192
193
    protected function isInferSchema()
194
    {
195
        return is_a($this->schema, 'frictionlessdata\\tableschema\\InferSchema');
196
    }
197
198
    protected function ensureInferredSchema($numPeekRows = 10)
199
    {
200
        if ($this->isInferSchema() && count($this->schema->fields()) == 0) {
201
            // need to fetch some rows first
202
            if ($numPeekRows > 0) {
203
                $i = 0;
204
                foreach ($this as $row) {
205
                    if (++$i > $numPeekRows) {
206
                        break;
207
                    }
208
                }
209
                // these rows will be returned by next current() call
210
                $this->castRows = $this->schema->lock();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class frictionlessdata\tableschema\Schema as the method lock() does only exist in the following sub-classes of frictionlessdata\tableschema\Schema: frictionlessdata\tableschema\InferSchema. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
211
            }
212
        }
213
    }
214
}
215