Passed
Push — master ( bc1c68...7768b1 )
by Fabrice
03:05
created

src/Extractors/File/FileExtractorAbstract.php (1 issue)

1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Extractors\File;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\NodalFlow\YaEtlException;
14
use fab2s\YaEtl\Extractors\ExtractorAbstract;
15
use fab2s\YaEtl\Traits\FileHandlerTrait;
16
17
/**
18
 * Class FileExtractorAbstract
19
 */
20
abstract class FileExtractorAbstract extends ExtractorAbstract
21
{
22
    use FileHandlerTrait;
23
24
    /**
25
     * @var string
26
     */
27
    protected $srcFile;
28
29
    /**
30
     * @param resource|string $input
31
     *
32
     * @throws YaEtlException
33
     * @throws NodalFlowException
34
     */
35
    public function __construct($input)
36
    {
37
        if (is_resource($input)) {
38
            $this->handle = $input;
39
        } elseif (is_file($input)) {
40
            $this->srcFile = $input;
41
        } else {
42
            throw new YaEtlException('Input is either not a resource or not a file');
43
        }
44
45
        parent::__construct();
46
    }
47
48
    /**
49
     * @param mixed $param
50
     *
51
     * @return bool
52
     */
53
    public function extract($param = null)
54
    {
55
        if ($this->srcFile !== null) {
56
            $this->handle = fopen($this->srcFile, 'rb');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($this->srcFile, 'rb') can also be of type false. However, the property $handle is declared as type resource. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
57
        }
58
59
        if (!is_resource($this->handle)) {
60
            return false;
61
        }
62
63
        $this->getCarrier()->getFlowMap()->incrementNode($this->getId(), 'num_extract');
64
65
        return rewind($this->handle);
66
    }
67
}
68