Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

Eccube/Doctrine/Common/CsvDataFixtures/Loader.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Doctrine\Common\CsvDataFixtures;
15
16
use Doctrine\Common\DataFixtures\FixtureInterface;
17
use Symfony\Component\Finder\Finder;
18
use Symfony\Component\Yaml\Yaml;
19
20
/**
21
 * CSVファイルのローダー.
22
 *
23
 * @see https://github.com/doctrine/data-fixtures/blob/master/lib/Doctrine/Common/DataFixtures/Loader.php
24
 */
25
class Loader
26
{
27
    /**
28
     * @var CsvFixture[]
29
     */
30
    protected $fixtures;
31
32
    /**
33
     * Load fixtures from directory.
34
     *
35
     * 同一階層に, Fixture のロード順を定義した definition.yml が必要.
36
     *
37
     * @param string $dir
38
     *
39
     * @return array fixtures.
40
     */
41 1
    public function loadFromDirectory($dir)
42
    {
43 1
        if (!dir($dir)) {
44
            throw new \InvalidArgumentException(sprintf('"%s" does not exist', $dir));
45
        }
46
47
        // import順序の定義ファイルを取得.
48 1
        $file = $dir.'/definition.yml';
49 1
        if (!file_exists($file)) {
50
            // 定義ファイルが存在しなければ取得した順序で処理
51
            $finder = Finder::create()
0 ignored issues
show
$finder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
                ->in($dir)
53
                ->name('*.csv');
54
        }
55 1
        $definition = Yaml::parse(file_get_contents($file));
56 1
        $definition = array_flip($definition);
57
58 1
        $finder = Finder::create()
59 1
            ->in($dir)
60 1
            ->name('*.csv')
61 1
            ->sort(
62
                // 定義ファイルに記載の順にソート.
63 1
                function (\SplFileInfo $a, \SplFileInfo $b) use ($definition) {
64 1
                    if (!isset($definition[$a->getFilename()])) {
65
                        throw new \Exception(sprintf('"%s" is undefined in %s', $a->getFilename()));
66
                    }
67 1
                    if (!isset($definition[$b->getFilename()])) {
68
                        throw new \Exception(sprintf('"%s" is undefined in %s', $b->getFilename()));
69
                    }
70
71 1
                    $a_sortNo = $definition[$a->getFilename()];
72 1
                    $b_sortNo = $definition[$b->getFilename()];
73
74 1
                    if ($a_sortNo < $b_sortNo) {
75 1
                        return -1;
76
                    } elseif ($a_sortNo > $b_sortNo) {
77
                        return 1;
78
                    } else {
79
                        return 0;
80
                    }
81 1
                }
82
            )
83 1
            ->files();
84
85 1
        return $this->loadFromIterator($finder->getIterator());
86
    }
87
88
    /**
89
     * Load fixtures from Iterator.
90
     *
91
     * @param \Iterator $Iterator Iterator of \SplFileInfo
92
     *
93
     * @return array fixtures.
94
     */
95 2
    public function loadFromIterator(\Iterator $Iterator)
96
    {
97 2
        $fixtures = [];
98 2
        foreach ($Iterator as $fixture) {
99
            // TODO $fixture が \SplFileInfo ではない場合の対応
100 2
            $CsvFixture = new CsvFixture($fixture->openFile());
101 2
            $this->addFixture($CsvFixture);
102 2
            $fixtures[] = $CsvFixture;
103
        }
104
105 2
        return $fixtures;
106
    }
107
108 1
    public function getFixtures()
109
    {
110 1
        return $this->fixtures;
111
    }
112
113 3
    public function addFixture(FixtureInterface $fixture)
114
    {
115 3
        $this->fixtures[] = $fixture;
116
    }
117
}
118