TimedExecutor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 31
c 2
b 0
f 0
dl 0
loc 68
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getExecuteFileProcess() 0 11 1
A getExecuteStatementProcess() 0 10 1
A resultSetToArray() 0 3 1
A getTimingData() 0 22 4
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Db3v4l\Core\SqlExecutor\Forked;
4
5
use Db3v4l\API\Interfaces\SqlExecutor\Forked\CommandExecutor;
6
use Db3v4l\API\Interfaces\SqlExecutor\Forked\FileExecutor;
7
use Db3v4l\API\Interfaces\TimedExecutor as TimedExecutorInterface;
8
9
class TimedExecutor implements CommandExecutor, FileExecutor, TimedExecutorInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class TimedExecutor
Loading history...
10
{
11
    /** @var CommandExecutor|FileExecutor */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
12
    protected $wrappedExecutor;
13
    protected $timingFile;
14
15
    protected $timeCmd = '/usr/bin/time';
16
17
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
18
     * @param CommandExecutor|FileExecutor $wrappedExecutor
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
19
     */
20
    public function __construct($wrappedExecutor)
21
    {
22
        $this->wrappedExecutor = $wrappedExecutor;
23
    }
24
25
    public function getExecuteStatementProcess($sql)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getExecuteStatementProcess()
Loading history...
26
    {
27
        $process = $this->wrappedExecutor->getExecuteStatementProcess($sql);
0 ignored issues
show
Bug introduced by
The method getExecuteStatementProcess() does not exist on Db3v4l\API\Interfaces\Sq...tor\Forked\FileExecutor. Since it exists in all sub-types, consider adding an abstract or default implementation to Db3v4l\API\Interfaces\Sq...tor\Forked\FileExecutor. ( Ignorable by Annotation )

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

27
        /** @scrutinizer ignore-call */ 
28
        $process = $this->wrappedExecutor->getExecuteStatementProcess($sql);
Loading history...
28
29
        // wrap in a `time` call
30
        $this->timingFile = tempnam(sys_get_temp_dir(), 'db3v4l_');
31
        $process->setCommandLine(
32
            $this->timeCmd . ' ' . escapeshellarg('--output=' . $this->timingFile) . ' ' . escapeshellarg('--format=%M %e') . ' '
33
            . $process->getCommandLine());
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
34
        return $process;
35
    }
36
37
    public function getExecuteFileProcess($sql)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getExecuteFileProcess()
Loading history...
38
    {
39
        $process = $this->wrappedExecutor->getExecuteFileProcess($sql);
0 ignored issues
show
Bug introduced by
The method getExecuteFileProcess() does not exist on Db3v4l\API\Interfaces\Sq...\Forked\CommandExecutor. Since it exists in all sub-types, consider adding an abstract or default implementation to Db3v4l\API\Interfaces\Sq...\Forked\CommandExecutor. ( Ignorable by Annotation )

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

39
        /** @scrutinizer ignore-call */ 
40
        $process = $this->wrappedExecutor->getExecuteFileProcess($sql);
Loading history...
40
41
        // wrap in a `time` call
42
        $this->timingFile = tempnam(sys_get_temp_dir(), 'db3v4l_');
43
        $process->setCommandLine(
44
            $this->timeCmd . ' ' . escapeshellarg('--output=' . $this->timingFile) . ' ' . escapeshellarg('--format=%M %e') . ' '
45
            . $process->getCommandLine());
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
46
47
        return $process;
48
    }
49
50
    public function resultSetToArray($data)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resultSetToArray()
Loading history...
51
    {
52
        return $this->wrappedExecutor->resultSetToArray($data);
53
    }
54
55
    public function getTimingData($onceIsEnough = true)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getTimingData()
Loading history...
56
    {
57
        if (!is_file($this->timingFile)) {
58
            throw new \Exception("File with timing data gone missing: '{$this->timingFile}'");
59
        }
60
61
        $timingData = file_get_contents($this->timingFile);
62
        if ($timingData != '') {
63
            $timingData = preg_replace('/Command exited with non-zero status [0-9]+/', '', $timingData);
64
            $timingData = explode(' ', trim($timingData), 2);
65
            $results['time'] = $timingData[1];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$results was never initialized. Although not strictly required by PHP, it is generally a good practice to add $results = array(); before regardless.
Loading history...
66
            $results['memory'] = $timingData[0];
67
        } else {
68
            // happens eg. if `time` command is not available
69
            throw new \Exception("File with timing data empty: '{$this->timingFile}'");
70
        }
71
72
        if ($onceIsEnough) {
73
            unlink ($this->timingFile);
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
74
        }
75
76
        return $results;
77
    }
78
}
79