Issues (1)

src/Benchmark.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\nanobench;
11
12
use Closure;
13
use Generator;
14
use loophp\nanobench\Time\StopwatchInterface;
15
use loophp\nanobench\Time\TimeInterface;
16
17
/**
18
 * @psalm-template T
19
 */
20
final class Benchmark implements BenchmarkInterface
21
{
22
    /**
23
     * @psalm-var list<T>
24
     */
25
    private array $arguments;
26
27
    /**
28
     * @psalm-var Closure(T...): T
29
     */
30
    private Closure $closure;
31
32
    /**
33
     * @var mixed|null
34
     * @psalm-var T|null
35
     */
36
    private $return;
37
38
    private StopwatchInterface $stopwatch;
39
40
    /**
41
     * @psalm-param Closure(T...): T $closure
42
     * @psalm-param T ...$arguments
43
     */
44 3
    public function __construct(StopwatchInterface $stopwatch, Closure $closure, ...$arguments)
45
    {
46 3
        $this->stopwatch = $stopwatch->reset();
47 3
        $this->closure = $closure;
48 3
        $this->arguments = $arguments;
49 3
    }
50
51 1
    public function getDuration(): TimeInterface
52
    {
53 1
        return $this->stopwatch->getDiffFromTo('start', 'stop');
54
    }
55
56
    /**
57
     * @return mixed|null
58
     * @psalm-return T|null
59
     */
60 1
    public function getReturn()
61
    {
62 1
        return $this->return;
63
    }
64
65 1
    public function getStopwatch(): StopwatchInterface
66
    {
67 1
        return $this->stopwatch;
68
    }
69
70 1
    public function run(): BenchmarkInterface
71
    {
72 1
        [,$return,] = iterator_to_array($this->benchRunner());
73
74 1
        $this->return = $return;
75
76 1
        return $this;
77
    }
78
79 1
    private function benchRunner(): Generator
80
    {
81 1
        yield $this->stopwatch->start();
82
83 1
        yield ($this->closure)(...$this->arguments);
84
85 1
        yield $this->stopwatch->stop();
0 ignored issues
show
Are you sure the usage of $this->stopwatch->stop() targeting loophp\nanobench\Time\StopwatchInterface::stop() 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...
86 1
    }
87
}
88