Test Setup Failed
Push — master ( f92f3f...8ec8d5 )
by Php Easy Api
04:10
created

Track::log()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 18
nop 0
dl 0
loc 38
rs 8.6346
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Console\Source\Track;
4
5
use Resta\Console\ConsoleOutputter;
6
use Resta\Console\ConsoleListAccessor;
7
8
class Track extends ConsoleOutputter
9
{
10
    use ConsoleListAccessor;
11
12
    /**
13
     * @var $type
0 ignored issues
show
Documentation Bug introduced by
The doc comment $type at position 0 could not be parsed: Unknown type name '$type' at position 0 in $type.
Loading history...
14
     */
15
    public $type = 'track';
16
17
    /**
18
     * @var $define
0 ignored issues
show
Documentation Bug introduced by
The doc comment $define at position 0 could not be parsed: Unknown type name '$define' at position 0 in $define.
Loading history...
19
     */
20
    public $define = 'returns track information for application';
21
22
    /**
23
     * @var $commandRule
0 ignored issues
show
Documentation Bug introduced by
The doc comment $commandRule at position 0 could not be parsed: Unknown type name '$commandRule' at position 0 in $commandRule.
Loading history...
24
     */
25
    public $commandRule = [];
26
27
    /**
28
     * @return mixed|void
29
     */
30
    public function log()
31
    {
32
        if(app()->has('track.path')){
33
            $trackPath = app()->get('track.path');
34
            $logger = $trackPath($this->argument);
35
        }
36
        else{
37
            $logger = app()->path()->appLog().''.DIRECTORY_SEPARATOR.''.date('Y').''.DIRECTORY_SEPARATOR.''.date('m').''.DIRECTORY_SEPARATOR.''.date('d').'-access.log';
38
        }
39
40
        if(!file_exists($logger)){
41
            echo 'No requests for a log tracker have been detected yet.';
42
            echo PHP_EOL;
43
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
44
        }
45
46
        $tailCommand = 'tail -n 1 -f '.escapeshellarg($logger).'';
47
48
        while (@ ob_end_flush()); // end all output buffers if any
49
50
        $proc = popen($tailCommand, 'r');
51
52
        $number = 0;
53
        while (!feof($proc))
0 ignored issues
show
Bug introduced by
It seems like $proc 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

53
        while (!feof(/** @scrutinizer ignore-type */ $proc))
Loading history...
54
        {
55
            $result = fread($proc, 4096);
0 ignored issues
show
Bug introduced by
It seems like $proc can also be of type false; however, parameter $handle of fread() 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

55
            $result = fread(/** @scrutinizer ignore-type */ $proc, 4096);
Loading history...
56
            if(preg_match('@\{(.*)\}@',$result,$output)){
57
                $outputArray = json_decode($output[0],1);
58
59
                $outputArray['trackNumber'] = ++$number;
60
61
                if(app()->has('track.log')){
62
63
                    $track = app()->get('track.log');
64
                    echo $track($outputArray,$this->argument);
65
                }
66
            }
67
            @ flush();
0 ignored issues
show
Bug introduced by
Are you sure the usage of flush() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for flush(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

67
            /** @scrutinizer ignore-unhandled */ @ flush();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
68
        }
69
70
    }
71
}