Completed
Pull Request — master (#263)
by AntikCz
24:54
created

CsvExport   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 71
ccs 44
cts 44
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C printData() 0 49 12
A setHttpHeaders() 0 7 1
1
<?php
2
3
namespace Grido\Components\Exports;
4
5
use Grido\Components\Columns\Column;
6
use Nette\Http\IResponse;
7
8
class CsvExport extends BaseExport
9 1
{
10
11
    /** @deprecated */
12
    const CSV_ID = 'csv';
13
14
    /**
15
     * @return void
16
     */
17
    protected function printData()
18
    {
19 1
        $escape = function($value) {
20 1
            return preg_match("~[\"\n,;\t]~", $value) || $value === ""
21 1
                ? '"' . str_replace('"', '""', $value) . '"'
22 1
                : $value;
23 1
        };
24
25 1
        $print = function(array $row) {
26 1
            print implode(',', $row) . "\n";
27 1
        };
28
29 1
        $columns = $this->grid[Column::ID]->getComponents();
30
31 1
        $header = [];
32 1
        $headerItems = $this->header ? $this->header : $columns;
33 1
        foreach ($headerItems as $column) {
34 1
            $header[] = $this->header
35 1
                ? $escape($column)
36 1
                : $escape($column->getLabel());
37 1
        }
38
39 1
        $print($header);
40
41 1
        $datasource = $this->grid->getData(FALSE, FALSE, FALSE);
42 1
        $iterations = ceil($datasource->getCount() / $this->fetchLimit);
0 ignored issues
show
Bug introduced by
The method getCount does only exist in Grido\DataSources\IDataSource, but not in Nette\Database\Table\Selection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43 1
        for ($i = 0; $i < $iterations; $i++) {
44 1
            $datasource->limit($i * $this->fetchLimit, $this->fetchLimit);
45 1
            $data = $this->customData
46 1
                ? call_user_func_array($this->customData, [$datasource])
47 1
                : $datasource->getData();
0 ignored issues
show
Bug introduced by
The method getData does only exist in Grido\DataSources\IDataSource, but not in Nette\Database\Table\Selection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
49 1
            foreach ($data as $items) {
50 1
                $row = [];
51
52 1
                $columns = $this->customData
53 1
                    ? $items
54 1
                    : $columns;
55
56 1
                foreach ($columns as $column) {
57 1
                    $row[] = $this->customData
58 1
                        ? $escape($column)
59 1
                        : $escape($column->renderExport($items));
60 1
                }
61
62 1
                $print($row);
63 1
            }
64 1
        }
65 1
    }
66
67
    /**
68
     * @param IResponse $httpResponse
69
     * @param string $label
70
     */
71
    protected function setHttpHeaders(IResponse $httpResponse, $label)
72
    {
73 1
        $encoding = 'utf-8';
74 1
        $httpResponse->setHeader('Content-Encoding', $encoding);
75 1
        $httpResponse->setHeader('Content-Type', "text/csv; charset=$encoding");
76 1
        $httpResponse->setHeader('Content-Disposition', "attachment; filename=\"$label.csv\"");
77 1
    }
78
}
79