Completed
Push — feature-data-files ( f34d72 )
by Arnaud
03:53
created

DataLoad::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Cecil\Util;
12
use Symfony\Component\Yaml\Yaml;
13
use Symfony\Component\Finder\Finder;
14
15
/**
16
 * Load data files.
17
 */
18
class DataLoad extends AbstractStep
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function init($options)
24
    {
25
        if (is_dir($this->builder->getConfig()->getDataPath())) {
26
            $this->process = true;
27
        }
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function process()
34
    {
35
        call_user_func_array($this->builder->getMessageCb(), ['DATA', 'Loading data']);
36
37
        $data = Finder::create()
38
            ->files()
39
            ->in($this->builder->getConfig()->getDataPath())
40
            ->name('/\.(' . implode('|', $this->builder->getConfig()->get('data.ext')) . ')$/')
41
            ->sortByName(true);
42
43
        $count = 0;
44
        $max = count($data);
45
46
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
47
        foreach ($data as $file) {
48
            $count++;
49
            set_error_handler(
50
                function ($severity, $message, $file, $line) {
51
                    throw new \ErrorException($message, 0, $severity, $file, $line, null);
1 ignored issue
show
Documentation introduced by
null is of type null, but the function expects a object<Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
                }
53
            );
54
            $dataFile = $file->getContents();
55
            restore_error_handler();
56
            $dataArray = Yaml::parse($dataFile);
57
            $message = sprintf('"%s" loaded', $file->getBasename());
58
            $dataArray = array_merge_recursive(
59
                $this->builder->getData(),
60
                [$file->getBasename('.' . $file->getExtension()) => $dataArray]
61
            );
62
            $this->builder->setData($dataArray);
63
64
            call_user_func_array($this->builder->getMessageCb(), ['DATA_PROGRESS', $message, $count, $max]);
65
        }
66
    }
67
}
68