Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Parser/Middleware/StopwatchMiddleware.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Parser\Middleware;
17
18
use phpDocumentor\Reflection\Middleware\Command;
19
use phpDocumentor\Reflection\Middleware\Middleware;
20
use Psr\Log\LoggerInterface;
21
use Psr\Log\LogLevel;
22
use Symfony\Component\Stopwatch\Stopwatch;
23
24
final class StopwatchMiddleware implements Middleware
25
{
26
    /** @var int $memory amount of memory used */
27
    private $memory = 0;
28
29
    /**
30
     * @var Stopwatch
31
     */
32
    private $stopwatch;
33
    private $logger;
34
35
    /**
36
     * StopwatchMiddleware constructor.
37
     */
38
    public function __construct(Stopwatch $stopwatch, LoggerInterface $logger)
39
    {
40
        $this->stopwatch = $stopwatch;
41
        $this->logger = $logger;
42
    }
43
44
    /**
45
     * Executes this middle ware class.
46
     *
47
     * @param callable $next
48
     * @return object
49
     */
50
    public function execute(Command $command, callable $next)
51
    {
52
        $result = $next($command);
53
54
        if ($this->stopwatch) {
55
            $lap = $this->stopwatch->lap('parser.parse');
56
            $oldMemory = $this->memory;
57
            $periods = $lap->getPeriods();
58
            $memory = end($periods)->getMemory();
59
60
            $this->log(
61
                '>> Memory after processing of file: ' . number_format($memory / 1024 / 1024, 2)
62
                . ' megabytes (' . (($memory - $oldMemory >= 0)
63
                    ? '+'
64
                    : '-') . number_format(($memory - $oldMemory) / 1024)
65
                . ' kilobytes)',
66
                LogLevel::DEBUG
67
            );
68
69
            $this->memory = $memory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $memory can also be of type double. However, the property $memory is declared as type integer. 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...
70
        }
71
72
        return $result;
73
    }
74
75
    /**
76
     * Dispatches a logging request.
77
     *
78
     * @param string   $message  The message to log.
79
     * @param string   $priority The logging priority as declared in the LogLevel PSR-3 class.
80
     * @param string[] $parameters
81
     */
82
    private function log($message, $priority = LogLevel::INFO, $parameters = [])
83
    {
84
        $this->logger->log($priority, $message, $parameters);
85
    }
86
}
87