Test Failed
Push — master ( e4a428...eca95d )
by Ricardo
04:31
created

GetDataTrait::getFileUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Validate\Traits;
4
5
/**
6
 * Undocumented trait
7
 */
8
trait GetDataTrait
9
{
10
11
    /**
12
     * Get full file url
13
     *
14
     * @param string $file
15
     * @return string
16
     */
17
    public static function getFileUrl($file)
18
    {
19
        $rootDir = DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."data";
20
        return dirname(__FILE__).$rootDir.DIRECTORY_SEPARATOR.$file;
21
    }
22
23
    /**
24
     * Return array from file
25
     *
26
     * @param string $file
27
     * @return array
28
     */
29
    public static function getListFromFile($file)
30
    {
31
        return file(self::getFileUrl($file));
0 ignored issues
show
Bug Best Practice introduced by
The expression return file(self::getFileUrl($file)) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
32
    }
33
34
    /**
35
     * Found string inside file
36
     *
37
     * @param string $string
38
     * @param string $file
39
     * @return bool
40
     */
41
    public static function foundInFile($string, $file)
42
    {
43
        $fileHandle = fopen(self::getFileUrl($file), "r");
44
        while (!feof($fileHandle)) {
0 ignored issues
show
Bug introduced by
It seems like $fileHandle can also be of type false; however, parameter $handle of feof() does only seem to accept resource, 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

44
        while (!feof(/** @scrutinizer ignore-type */ $fileHandle)) {
Loading history...
45
            if (trim($string) === trim(fgets($fileHandle))){
0 ignored issues
show
Bug introduced by
It seems like $fileHandle can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, 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

45
            if (trim($string) === trim(fgets(/** @scrutinizer ignore-type */ $fileHandle))){
Loading history...
46
                return true;
47
            }
48
        }
49
        fclose($fileHandle);
0 ignored issues
show
Bug introduced by
It seems like $fileHandle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

49
        fclose(/** @scrutinizer ignore-type */ $fileHandle);
Loading history...
50
        return false;
51
    }
52
}