Completed
Push — master ( b10a48...29cde4 )
by Ori
05:27
created

Table::filterLine()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 4.909
c 0
b 0
f 0
cc 9
eloc 21
nc 10
nop 1
1
<?php
2
namespace frictionlessdata\tableschema;
3
4
/**
5
 * represents a data source which validates against a table schema
6
 * provides interfaces for validating the data and iterating over it
7
 * casts all values to their native values according to the table schema
8
 */
9
class Table implements \Iterator
10
{
11
    /**
12
     * @param DataSources\DataSourceInterface $dataSource
13
     * @param Schema $schema
14
     * @throws Exceptions\DataSourceException
15
     */
16
    public function __construct($dataSource, $schema)
17
    {
18
        $this->dataSource = $dataSource;
19
        $this->schema = $schema;
20
        $this->dataSource->open();
21
    }
22
23
    /**
24
     * @param DataSources\DataSourceInterface $dataSource
25
     * @param Schema $schema
26
     * @param int $numPeekRows
27
     * @return array of validation errors
28
     */
29
    public static function validate($dataSource, $schema, $numPeekRows=10)
30
    {
31
        try {
32
            $table = new static($dataSource, $schema);
33
        } catch (Exceptions\DataSourceException $e) {
34
            return [new SchemaValidationError(SchemaValidationError::LOAD_FAILED, $e->getMessage())];
35
        };
36
        if ($numPeekRows > 0) {
37
            $i = 0;
38
            try {
39
                foreach ($table as $row) {
40
                    if (++$i > $numPeekRows) break;
41
                }
42
            } 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...
43
                // general error in getting the next row from the data source
44
                return [new SchemaValidationError(SchemaValidationError::ROW_VALIDATION, [
45
                    "row" => $i,
46
                    "error" => $e->getMessage()
47
                ])];
48
            } catch (Exceptions\FieldValidationException $e) {
49
                // validation error in one of the fields
50
                return array_map(function($validationError) use ($i) {
51
                    return new SchemaValidationError(SchemaValidationError::ROW_FIELD_VALIDATION, [
52
                        "row" => $i+1,
53
                        "field" => $validationError->extraDetails["field"],
54
                        "error" => $validationError->extraDetails["error"],
55
                        "value" => $validationError->extraDetails["value"],
56
                    ]);
57
                }, $e->validationErrors);
58
            }
59
        }
60
        return [];
61
    }
62
63
    /**
64
     * called on each iteration to get the next row
65
     * does validation and casting on the row
66
     * @return mixed[]
67
     * @throws Exceptions\FieldValidationException
68
     * @throws Exceptions\DataSourceException
69
     */
70
    public function current() {
71
        return $this->schema->castRow($this->dataSource->getNextLine());
72
    }
73
74
    // not interesting, standard iterator functions
75
    // to simplify we prevent rewinding - so you can only iterate once
76
    public function __destruct() {$this->dataSource->close();}
77
    public function rewind() {if ($this->currentLine == 0) {$this->currentLine = 1;} else {throw new \Exception("rewind is not supported");}}
78
    public function key() {return $this->currentLine;}
79
    public function next() {$this->currentLine++;}
80
    public function valid() {return !$this->dataSource->isEof();}
81
82
    protected $currentLine = 0;
83
    protected $dataSource;
84
    protected $schema;
85
}