Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PhpStan suggestions #378

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4snapshot

env:
- PHP_BIN=php
Expand Down Expand Up @@ -69,6 +70,7 @@ jobs:
allow_failures:
- stage: Static Analysis (informative)
- stage: Code Coverage
- php: 7.4snapshot


sudo: false
Expand Down
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
"ext-json": "*"
},
"require-dev": {
"nette/utils": "^2.4 || ^3.0",
"nette/di": "^2.4 || ~3.0.0",
"nette/utils": "^3.0",
"nette/di": "^3.0",
"nette/tester": "^2.2",
"nette/mail": "^3.0",
"nette/http": "^3.0",
"latte/latte": "^2.5",
"psr/log": "^1.0"
},
"conflict": {
"nette/di": "<3.0"
},
"suggest": {
"https://nette.org/donate": "Please support Tracy via a donation"
},
Expand All @@ -35,7 +41,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.6-dev"
"dev-master": "2.7-dev"
}
}
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Alternatively, you can download the whole package or [tracy.phar](https://github

| Tracy | PHP | compatible with browsers
|-----------|---------------|----------
| Tracy 2.7 | PHP 7.1 – 7.3 | Chrome 49+, Firefox 45+, MS Edge 14+, Safari 10+ and iOS Safari 10.2+
| Tracy 2.6 | PHP 7.1 – 7.3 | Chrome 49+, Firefox 45+, MS Edge 14+, Safari 10+ and iOS Safari 10.2+
| Tracy 2.5 | PHP 5.4.4 – 7.3 | Chrome 49+, Firefox 45+, MS Edge 12+, Safari 10+ and iOS Safari 10.2+
| Tracy 2.4 | PHP 5.4.4 – 7.2 | Chrome 29+, Firefox 28+, IE 11+ (except AJAX), MS Edge 12+, Safari 9+ and iOS Safari 9.2+
Expand Down
8 changes: 5 additions & 3 deletions src/Bridges/Nette/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public static function renderLatteError(?\Throwable $e): ?array
. BlueScreen::highlightLine(htmlspecialchars($e->sourceCode, ENT_IGNORE, 'UTF-8'), $e->sourceLine)
. '</div></pre>',
];

} elseif ($e && strpos($file = $e->getFile(), '.latte--')) {
}
if ($e && strpos($file = $e->getFile(), '.latte--')) {
janbarasek marked this conversation as resolved.
Show resolved Hide resolved
$lines = file($file);
if (preg_match('#// source: (\S+\.latte)#', $lines[1], $m) && @is_file($m[1])) { // @ - may trigger error
$templateFile = $m[1];
Expand All @@ -68,6 +68,7 @@ public static function renderLatteError(?\Throwable $e): ?array

public static function renderLatteUnknownMacro(?\Throwable $e): ?array
{
assert(\class_exists(Latte\CompileException::class));
janbarasek marked this conversation as resolved.
Show resolved Hide resolved
if (
$e instanceof Latte\CompileException
&& @is_file($e->sourceName) // @ - may trigger error
Expand All @@ -94,7 +95,8 @@ public static function renderMemberAccessException(?\Throwable $e): ?array
'link' => Helpers::editorUri($loc['file'], $loc['line'], 'fix', '->' . $m[1], '->' . $m[2]),
'label' => 'fix it',
];
} elseif (preg_match('#Call to undefined (static )?method .+::(\w+)\(\), did you mean (\w+)\(\)?#A', $e->getMessage(), $m)) {
}
if (preg_match('#Call to undefined (static )?method .+::(\w+)\(\), did you mean (\w+)\(\)?#A', $e->getMessage(), $m)) {
$operator = $m[1] ? '::' : '->';
return [
'link' => Helpers::editorUri($loc['file'], $loc['line'], 'fix', $operator . $m[2] . '(', $operator . $m[3] . '('),
Expand Down
67 changes: 36 additions & 31 deletions src/Bridges/Nette/TracyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Tracy\Bridges\Nette;

use Nette;
use Nette\Schema\Expect;
use Tracy;


Expand All @@ -18,26 +19,6 @@
*/
class TracyExtension extends Nette\DI\CompilerExtension
{
public $defaults = [
'email' => null,
'fromEmail' => null,
'logSeverity' => null,
'editor' => null,
'browser' => null,
'errorTemplate' => null,
'strictMode' => null,
'showBar' => null,
'maxLen' => null,
'maxLength' => null,
'maxDepth' => null,
'showLocation' => null,
'scream' => null,
'bar' => [], // of class name
'blueScreen' => [], // of callback
'editorMapping' => [],
'netteMailer' => true,
];

/** @var bool */
private $debugMode;

Expand All @@ -52,9 +33,31 @@ public function __construct(bool $debugMode = false, bool $cliMode = false)
}


public function getConfigSchema(): Nette\Schema\Schema
{
return Expect::structure([
'email' => Expect::email()->dynamic(),
'fromEmail' => Expect::email()->dynamic(),
'logSeverity' => Expect::scalar(),
'editor' => Expect::string()->dynamic(),
'browser' => Expect::string()->dynamic(),
'errorTemplate' => Expect::string()->dynamic(),
'strictMode' => Expect::bool()->dynamic(),
'showBar' => Expect::bool()->dynamic(),
'maxLength' => Expect::int()->dynamic(),
'maxDepth' => Expect::int()->dynamic(),
'showLocation' => Expect::bool()->dynamic(),
'scream' => Expect::bool()->dynamic(),
'bar' => Expect::listOf('class|Nette\DI\Definitions\Statement'),
'blueScreen' => Expect::listOf('callable'),
'editorMapping' => Expect::arrayOf('string')->dynamic(),
'netteMailer' => Expect::bool(true),
]);
}


public function loadConfiguration()
{
$this->validateConfig($this->defaults);
$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('logger'))
Expand All @@ -74,7 +77,7 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
$initialize = $class->getMethod('initialize');
$builder = $this->getContainerBuilder();

$options = $this->config;
$options = (array) $this->config;
unset($options['bar'], $options['blueScreen'], $options['netteMailer']);
if (isset($options['logSeverity'])) {
$res = 0;
Expand All @@ -94,21 +97,23 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
}

$logger = $builder->getDefinition($this->prefix('logger'));
if ($logger->getFactory()->getEntity() !== [Tracy\Debugger::class, 'getLogger']) {
if (!$logger instanceof Nette\DI\ServiceDefinition || $logger->getFactory()->getEntity() !== [Tracy\Debugger::class, 'getLogger']) {
$initialize->addBody($builder->formatPhp('Tracy\Debugger::setLogger(?);', [$logger]));
}
if ($this->config['netteMailer'] && $builder->getByType(Nette\Mail\IMailer::class)) {
if ($this->config->netteMailer && $builder->getByType(Nette\Mail\IMailer::class)) {
$initialize->addBody($builder->formatPhp('Tracy\Debugger::getLogger()->mailer = ?;', [
[new Nette\DI\Statement(Tracy\Bridges\Nette\MailSender::class, ['fromEmail' => $this->config['fromEmail']]), 'send'],
[new Nette\DI\Statement(Tracy\Bridges\Nette\MailSender::class, ['fromEmail' => $this->config->fromEmail]), 'send'],
]));
}

if ($this->debugMode) {
foreach ((array) $this->config['bar'] as $item) {
if (is_string($item) && substr($item, 0, 1) === '@') {
$item = new Nette\DI\Statement(['@' . $builder::THIS_CONTAINER, 'getService'], [substr($item, 1)]);
} elseif (is_string($item)) {
$item = new Nette\DI\Statement($item);
foreach ($this->config->bar as $item) {
if (is_string($item)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in original implementation when you verify variable $item is string, second elseif condition does not make sense.

if ($item[0] ?? '' === '@') {
$item = new Nette\DI\Statement(['@' . $builder::THIS_CONTAINER, 'getService'], [substr($item, 1)]);
} else {
$item = new Nette\DI\Statement($item);
}
}
$initialize->addBody($builder->formatPhp(
'$this->getService(?)->addPanel(?);',
Expand All @@ -122,7 +127,7 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
}
}

foreach ((array) $this->config['blueScreen'] as $item) {
foreach ($this->config->blueScreen as $item) {
$initialize->addBody($builder->formatPhp(
'$this->getService(?)->addPanel(?);',
Nette\DI\Helpers::filterArguments([$this->prefix('blueScreen'), $item])
Expand Down
10 changes: 5 additions & 5 deletions src/Tracy/Bar/assets/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@

elem.querySelectorAll('.tracy-icons a').forEach((link) => {
link.addEventListener('click', (e) => {
if (link.rel === 'close') {
if (link.dataset.tracyAction === 'close') {
this.toPeek();
} else if (link.rel === 'window') {
} else if (link.dataset.tracyAction === 'window') {
this.toWindow();
}
e.preventDefault();
Expand Down Expand Up @@ -258,7 +258,7 @@
initTabs(elem) {
elem.querySelectorAll('a').forEach((link) => {
link.addEventListener('click', (e) => {
if (link.rel === 'close') {
if (link.dataset.tracyAction === 'close') {
this.close();

} else if (link.rel) {
Expand All @@ -284,7 +284,7 @@
});

link.addEventListener('mouseenter', (e) => {
if (e.buttons || !link.rel || link.rel === 'close' || elem.classList.contains('tracy-dragged')) {
if (e.buttons || !link.rel || elem.classList.contains('tracy-dragged')) {
return;
}

Expand All @@ -311,7 +311,7 @@
link.addEventListener('mouseleave', () => {
clearTimeout(this.displayTimeout);

if (link.rel && link.rel !== 'close' && !elem.classList.contains('tracy-dragged')) {
if (link.rel && !elem.classList.contains('tracy-dragged')) {
Debug.panels[link.rel].blur();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/Tracy/Bar/assets/bar.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ namespace Tracy;
<?php } endforeach ?>

<?php if ($type === 'main'): ?>
<li><a href="#" rel="close" title="close debug bar">&times;</a></li>
<li><a href="#" data-tracy-action="close" title="close debug bar">&times;</a></li>
<?php endif ?>
</ul>
4 changes: 2 additions & 2 deletions src/Tracy/Bar/assets/panels.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use Tracy\Helpers;

$icons = '
<div class="tracy-icons">
<a href="#" rel="window" title="open in window">&curren;</a>
<a href="#" rel="close" title="close window">&times;</a>
<a href="#" data-tracy-action="window" title="open in window">&curren;</a>
<a href="#" data-tracy-action="close" title="close window">&times;</a>
</div>
';

Expand Down
1 change: 0 additions & 1 deletion src/Tracy/BlueScreen/BlueScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ private function renderTemplate(\Throwable $exception, string $template, $toScre
);
$info = array_filter($this->info);
$source = Helpers::getSource();
$sourceIsUrl = preg_match('#^https?://#', $source);
$title = $exception instanceof \ErrorException
? Helpers::errorTypeToString($exception->getSeverity())
: Helpers::getClass($exception);
Expand Down
20 changes: 17 additions & 3 deletions src/Tracy/BlueScreen/assets/bluescreen.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
padding: 3px;
}

.tracy-bs-main {
display: flex;
flex-direction: column;
min-height: 100vh;
}

.tracy-bs-main.tracy-collapsed {
display: none;
}

#tracy-bs div.panel:last-of-type {
flex: 1;
}

#tracy-bs-error {
background: #CD1818;
color: white;
Expand Down Expand Up @@ -153,7 +167,7 @@
list-style: none;
}

#tracy-bs-logo a {
#tracy-bs .footer-logo a {
position: absolute;
bottom: 0;
right: 0;
Expand All @@ -165,8 +179,8 @@
margin: 0;
}

#tracy-bs-logo a:hover,
#tracy-bs-logo a:focus {
#tracy-bs .footer-logo a:hover,
#tracy-bs .footer-logo a:focus {
opacity: 1;
transition: opacity 0.1s;
}
Expand Down
20 changes: 9 additions & 11 deletions src/Tracy/BlueScreen/assets/content.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @param array $info
* @param string $title
* @param string $source
* @param bool $sourceIsUrl
* @param array $lastError
* @param array $httpHeaders
* @param callable $dump
Expand All @@ -28,7 +27,7 @@ $code = $exception->getCode() ? ' #' . $exception->getCode() : '';
?>
<div id="tracy-bs" itemscope>
<a id="tracy-bs-toggle" href="#" class="tracy-toggle"></a>
<div>
<div class="tracy-bs-main">
<div id="tracy-bs-error" class="panel">
<?php if ($exception->getMessage()): ?><p><?= Helpers::escapeHtml($title . $code) ?></p><?php endif ?>

Expand Down Expand Up @@ -357,15 +356,14 @@ $code = $exception->getCode() ? ' #' . $exception->getCode() : '';
</div></div>
<?php endforeach ?>


<ul>
<li><b><a href="https://nette.org/make-donation?to=tracy" target="_blank" rel="noreferrer noopener">Please support Tracy via a donation 💙️</a></b></li>
<li>Report generated at <?= @date('Y/m/d H:i:s') // @ timezone may not be set ?></li>
<li><?php if ($sourceIsUrl): ?><a href="<?= Helpers::escapeHtml($source) ?>"><?php endif ?><?= Helpers::escapeHtml($source) ?><?php if ($sourceIsUrl): ?></a><?php endif ?></li>
<?php foreach ($info as $item): ?><li><?= Helpers::escapeHtml($item) ?></li><?php endforeach ?>
</ul>

<div id="tracy-bs-logo"><a href="https://tracy.nette.org" rel="noreferrer"></a></div>
<footer>
<ul>
<li><b><a href="https://nette.org/make-donation?to=tracy" target="_blank" rel="noreferrer noopener">Please support Tracy via a donation 💙️</a></b></li>
<li>Report generated at <?= @date('Y/m/d H:i:s') // @ timezone may not be set ?></li>
<?php foreach ($info as $item): ?><li><?= Helpers::escapeHtml($item) ?></li><?php endforeach ?>
</ul>
<div class="footer-logo"><a href="https://tracy.nette.org" rel="noreferrer"></a></div>
</footer>
</div>
<meta itemprop=tracy-snapshot content=<?= Dumper::formatSnapshotAttribute($snapshot) ?>>
</div>
4 changes: 2 additions & 2 deletions src/Tracy/Debugger/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class Debugger
{
public const VERSION = '2.6.4';
public const VERSION = '2.7-dev';

/** server modes for Debugger::enable() */
public const
Expand All @@ -27,7 +27,7 @@ class Debugger

public const COOKIE_SECRET = 'tracy-debug';

/** @var bool in production mode is suppressed any debugging output */
/** @var bool|null in production mode is suppressed any debugging output */
public static $productionMode = self::DETECT;

/** @var bool whether to display debug bar in development mode */
Expand Down
5 changes: 3 additions & 2 deletions src/Tracy/Dumper/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ private function asHtml($var): string
' title="%in file % on line %" data-tracy-href="%"', "$code\n", $file, $line, Helpers::editorUri($file, $line)
) : null;

$options = [];
if (is_array($this->snapshot)) {
$options[self::SNAPSHOT] = &$this->snapshot;
}
Expand Down Expand Up @@ -517,7 +518,7 @@ public static function encodeString(string $s, int $maxLength = null): string
$i = $len = 0;
$maxI = $maxLength * 4; // max UTF-8 length
do {
if (($s[$i] < "\x80" || $s[$i] >= "\xC0") && (++$len > $maxLength) || $i >= $maxI) {
if (($s[$i] < "\x80" || $s[$i] >= "\xC0") && ((++$len > $maxLength) || $i >= $maxI)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operations priority might differ from what programmer expect. Brackets are safe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's not related the phpstan error it should not be part of this PR

$s = substr($s, 0, $i);
$shortened = true;
break;
Expand All @@ -539,7 +540,7 @@ public static function encodeString(string $s, int $maxLength = null): string


/**
* @param int|string $k
* @param int|string $key
* @return int|string
*/
private function encodeKey($key)
Expand Down
Loading