Issues (836)

framework/test/FileFixtureTrait.php (2 issues)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\test;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * FileFixtureTrait provides functionalities for loading data fixture from file.
15
 *
16
 * @author Leandro Guindani Gehlen <[email protected]>
17
 * @since 2.0.14
18
 */
19
trait FileFixtureTrait
20
{
21
    /**
22
     * @var string the directory path or [path alias](guide:concept-aliases) that contains the fixture data
23
     */
24
    public $dataDirectory;
25
    /**
26
     * @var string|bool the file path or [path alias](guide:concept-aliases) of the data file that contains the fixture data
27
     * to be returned by [[getData()]]. You can set this property to be false to prevent loading any data.
28
     */
29
    public $dataFile;
30
31
    /**
32
     * Returns the fixture data.
33
     *
34
     * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
35
     * The file should return the data array that will be stored in [[data]] after inserting into the database.
36
     *
37
     * @param string $file the data file path
38
     * @param bool $throwException whether to throw exception if fixture data file does not exist.
39
     * @return array the data to be put into the database
40
     * @throws InvalidConfigException if the specified data file does not exist.
41
     */
42 3
    protected function loadData($file, $throwException = true)
43
    {
44 3
        if ($file === null || $file === false) {
0 ignored issues
show
The condition $file === false is always false.
Loading history...
45 1
            return [];
46
        }
47
48 2
        if (basename($file) === $file && $this->dataDirectory !== null) {
49
            $file = $this->dataDirectory . '/' . $file;
50
        }
51
52 2
        $file = Yii::getAlias($file);
53 2
        if (is_file($file)) {
0 ignored issues
show
It seems like $file can also be of type false; however, parameter $filename of is_file() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        if (is_file(/** @scrutinizer ignore-type */ $file)) {
Loading history...
54 1
            return require $file;
55
        }
56
57 1
        if ($throwException) {
58 1
            throw new InvalidConfigException("Fixture data file does not exist: {$file}");
59
        }
60
61
        return [];
62
    }
63
}
64