Export::setDelimiter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 2
b 0
f 1
1
<?php
2
3
namespace DoctrineDatatable;
4
5
use Doctrine\ORM\Query;
6
7
class Export
8
{
9
    /**
10
     * @var Datatable
11
     */
12
    private $datatable;
13
14
    /**
15
     * @var resource
16
     */
17
    private $resource;
18
19
    /**
20
     * @var string
21
     */
22
    private $delimiter;
23
24
    public function __construct()
25
    {
26
        $this->setDelimiter("\t");
27
    }
28
29
    /**
30
     * PRIVATE METHODS.
31
     */
32
33
    /**
34
     * @author Mathieu Petrini <[email protected]>
35
     */
36
    private function data(): void
37
    {
38
        foreach ($this->datatable->createFinalQuery(array(
39
            'start' => 0,
40
            'limit' => INF, ))->getQuery()->iterate(null, Query::HYDRATE_SCALAR) as $row) {
41
            $line = $row[0];
42
            fwrite($this->resource, implode($this->delimiter, $line));
43
        }
44
        rewind($this->resource);
45
    }
46
47
    /**
48
     * PUBLIC METHODS.
49
     */
50
51
    /**
52
     * @author Mathieu Petrini <[email protected]>
53
     *
54
     * @return resource
55
     */
56
    public function export()
57
    {
58
        $this->resource = tmpfile();
0 ignored issues
show
Documentation Bug introduced by
It seems like tmpfile() can also be of type false. However, the property $resource is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
        $this->data();
60
61
        return $this->resource;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->resource could also return false which is incompatible with the documented return type resource. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
62
    }
63
64
    /**
65
     * GETTERS / SETTERS.
66
     */
67
68
    /**
69
     * @param Datatable $datatable
70
     *
71
     * @return Export
72
     */
73
    public function setDatatable(Datatable $datatable): self
74
    {
75
        $this->datatable = $datatable;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param string $delimiter
82
     *
83
     * @return Export
84
     */
85
    public function setDelimiter(string $delimiter): self
86
    {
87
        $this->delimiter = $delimiter;
88
89
        return $this;
90
    }
91
}
92