1 | <?php |
||
2 | |||
3 | namespace Jackal\Copycat\Writer; |
||
4 | |||
5 | use Exception; |
||
6 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||
7 | |||
8 | /** |
||
9 | * Class CSVFileWriter |
||
10 | * @package Jackal\Copycat\Writer |
||
11 | */ |
||
12 | class CSVFileWriter implements WriterInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $outputFilePathname; |
||
18 | |||
19 | /** |
||
20 | * @var resource |
||
21 | */ |
||
22 | protected $handle; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $index = 0; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $options = []; |
||
33 | |||
34 | /** |
||
35 | * CSVFileWriter constructor. |
||
36 | * @param $outputFilePathname |
||
37 | * @param array $options |
||
38 | */ |
||
39 | public function __construct($outputFilePathname, array $options = []) |
||
40 | { |
||
41 | $resolver = new OptionsResolver(); |
||
42 | $resolver->setDefaults([ |
||
43 | 'replace_file' => false, |
||
44 | 'delimiter' => ',', |
||
45 | 'enclosure' => '"', |
||
46 | 'header' => true, |
||
47 | 'columns' => [], |
||
48 | ]); |
||
49 | |||
50 | $this->options = $resolver->resolve($options); |
||
51 | |||
52 | $this->outputFilePathname = $outputFilePathname; |
||
53 | |||
54 | if (!is_dir(dirname($this->outputFilePathname))) { |
||
55 | mkdir(dirname($this->outputFilePathname), 0775, true); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @throws Exception |
||
61 | */ |
||
62 | public function prepare() |
||
63 | { |
||
64 | if (!is_dir(dirname($this->outputFilePathname))) { |
||
65 | mkdir(dirname($this->outputFilePathname), 0775, true); |
||
66 | } |
||
67 | |||
68 | $fileExists = file_exists($this->outputFilePathname); |
||
69 | if ($fileExists and !$this->options['replace_file']) { |
||
70 | throw new Exception('File ' . realpath($this->outputFilePathname) . ' already exists'); |
||
71 | } |
||
72 | |||
73 | if ($fileExists) { |
||
74 | unlink($this->outputFilePathname); |
||
75 | } |
||
76 | |||
77 | $this->handle = fopen($this->outputFilePathname, 'w'); |
||
0 ignored issues
–
show
|
|||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param array $item |
||
82 | */ |
||
83 | public function writeItem(array $item) |
||
84 | { |
||
85 | if ($this->options['columns'] and array_keys($item) != $this->options['columns']) { |
||
86 | $itemOrdered = []; |
||
87 | foreach ($this->options['columns'] as $orderedColumn) { |
||
88 | if (!array_key_exists($orderedColumn, $item)) { |
||
89 | $itemOrdered[$orderedColumn] = null; |
||
90 | } else { |
||
91 | $itemOrdered[$orderedColumn] = $item[$orderedColumn]; |
||
92 | } |
||
93 | } |
||
94 | $item = $itemOrdered; |
||
95 | } |
||
96 | |||
97 | if ($this->index == 0 and $this->options['header']) { |
||
98 | fputcsv($this->handle, array_keys($item), $this->options['delimiter'], $this->options['enclosure']); |
||
99 | } |
||
100 | fputcsv($this->handle, array_values($item), $this->options['delimiter'], $this->options['enclosure']); |
||
101 | $this->index++; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * |
||
106 | */ |
||
107 | public function finish() |
||
108 | { |
||
109 | fclose($this->handle); |
||
110 | } |
||
111 | } |
||
112 |
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 theid
property of an instance of theAccount
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.