Skip to content

Commit 78edcff

Browse files
authored
Add templates to static analyse (#150)
1 parent ba1e1f0 commit 78edcff

File tree

11 files changed

+72
-45
lines changed

11 files changed

+72
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 4.1.1 under development
44

5-
- no changes in this release.
5+
- Enh #150: Cleanup templates, remove legacy code (@vjik)
66

77
## 4.1.0 April 18, 2025
88

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@
4949
"require-dev": {
5050
"bamarni/composer-bin-plugin": "^1.8.2",
5151
"httpsoft/http-message": "^1.1.6",
52-
"phpunit/phpunit": "^10.5.45",
52+
"phpunit/phpunit": "^10.5.51",
5353
"psr/event-dispatcher": "^1.0",
54-
"rector/rector": "^2.0.11",
54+
"rector/rector": "^2.1.3",
5555
"roave/infection-static-analysis-plugin": "^1.35",
5656
"spatie/phpunit-watcher": "^1.24",
57-
"vimeo/psalm": "^5.26.1 || ^6.9.1",
58-
"yiisoft/di": "^1.3",
57+
"vimeo/psalm": "^5.26.1 || ^6.13.1",
58+
"yiisoft/di": "^1.4",
5959
"yiisoft/test-support": "^3.0.2"
6060
},
6161
"autoload": {

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
>
1212
<projectFiles>
1313
<directory name="src" />
14+
<directory name="templates" />
1415
<ignoreFiles>
1516
<directory name="vendor" />
1617
</ignoreFiles>

src/Exception/ErrorException.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@
1414

1515
/**
1616
* `ErrorException` represents a PHP error.
17-
* @psalm-type DebugBacktraceType = list<array{args?:list<mixed>,class?:class-string,file?:string,function:string,line?:int,object?:object,type?:string}>
17+
*
18+
* @psalm-type DebugBacktraceType = list<array{
19+
* args?: array<mixed>,
20+
* class?: class-string,
21+
* file?: string,
22+
* function?: string,
23+
* line?: int,
24+
* object?: object,
25+
* type?: string
26+
* }>
1827
*
1928
* @final
2029
*/
@@ -121,7 +130,6 @@ private function addXDebugTraceToFatalIfAvailable(): void
121130
}
122131

123132
$ref = new ReflectionProperty(Exception::class, 'trace');
124-
$ref->setAccessible(true);
125133
$ref->setValue($this, $trace);
126134
}
127135
}

src/Renderer/HtmlRenderer.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ final class HtmlRenderer implements ThrowableRendererInterface
8686
private readonly int $maxTraceLines;
8787

8888
/**
89-
* @var string|null The trace header line with placeholders to be be substituted. Defaults to null.
89+
* @var string|null The trace header line with placeholders to be substituted. Defaults to null.
9090
*
9191
* The placeholders are {file}, {line} and {icon}. A typical use case is the creation of IDE-specific links,
9292
* since when you click on a trace header link, it opens directly in the IDE. You can also insert custom content.
@@ -97,7 +97,7 @@ final class HtmlRenderer implements ThrowableRendererInterface
9797
* <a href="ide://open?file={file}&line={line}">{icon}</a>
9898
* ```
9999
*/
100-
private readonly ?string $traceHeaderLine;
100+
public readonly ?string $traceHeaderLine;
101101

102102
/**
103103
* @var string[]|null The list of vendor paths is determined automatically.
@@ -632,8 +632,6 @@ private function groupVendorCallStackItems(array $items): array
632632
$groupedItems[$groupIndex][$index] = $item;
633633
}
634634

635-
/** @psalm-var array<int, array<int, string>> $groupedItems It's needed for Psalm <=4.30 only. */
636-
637635
return $groupedItems;
638636
}
639637

templates/_call-stack-item.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* @var array $args
1515
* @var bool $isVendorFile
1616
* @var ReflectionMethod[] $reflectionParameters
17-
* @var HtmlRenderer $this
1817
*/
1918

2019
$icon = <<<HTML
@@ -24,16 +23,20 @@
2423
<path d="M16,5h1.58l-6.29,6.28a1,1,0,0,0,0,1.42,1,1,0,0,0,1.42,0L19,6.42V8a1,1,0,0,0,1,1h0a1,1,0,0,0,1-1V4a1,1,0,0,0-1-1L16,3h0a1,1,0,0,0,0,2Z" />
2524
</svg>
2625
HTML;
26+
27+
/**
28+
* @var HtmlRenderer $this
29+
*/
2730
?>
2831
<li class="<?= (!empty($lines) && ($index === 1 || !$isVendorFile)) ? 'application ' : '' ?>call-stack-item"
29-
data-line="<?= (int) ($line - $begin) ?>">
32+
data-line="<?= (int) $line - $begin ?>">
3033
<div class="element-wrap">
3134
<div class="flex-1 mw-100">
3235
<?php if ($file !== null): ?>
3336
<span class="file-name">
3437
<?= "{$index}. in {$this->htmlEncode($file)}" ?>
3538
<?php if ($this->traceHeaderLine !== null): ?>
36-
<?= strtr($this->traceHeaderLine, ['{file}' => $file, '{line}' => $line + 1, '{icon}' => $icon]) ?>
39+
<?= strtr($this->traceHeaderLine, ['{file}' => $file, '{line}' => (int) $line + 1, '{icon}' => $icon]) ?>
3740
<?php endif ?>
3841
</span>
3942
<?php endif ?>
@@ -75,7 +78,7 @@
7578
$<?= $this->htmlEncode(
7679
is_int($key) && isset($reflectionParameters[$key])
7780
? $reflectionParameters[$key]->getName()
78-
: $key
81+
: (string) $key
7982
) ?>
8083
</td>
8184
<td class="functionArguments_type">

templates/_call-stack-items.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
<?php
2-
/* @var $applicationItems array<int, string> */
3-
/* @var $vendorItemGroups array<int, array<int, string>> */
4-
/* @var $this \Yiisoft\ErrorHandler\Renderer\HtmlRenderer */
5-
$insertItem = static function (array &$items, array $item, int $offset = 0): void {
6-
$itemIndex = array_key_first($item);
7-
foreach (array_keys($items) as $index) {
8-
$offset++;
9-
if ($index === ($itemIndex - 1)) {
10-
break;
2+
3+
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;
4+
5+
/**
6+
* @psalm-var array<int, string> $applicationItems
7+
* @psalm-var array<int, array<int, string>> $vendorItemGroups
8+
* @var HtmlRenderer $this
9+
*/
10+
11+
$insertItem =
12+
/**
13+
* @psalm-param array<int, string> $items
14+
* @psalm-param non-empty-array<int, string> $item
15+
*/
16+
static function (array &$items, array $item, int $offset = 0): void {
17+
$itemIndex = array_key_first($item);
18+
foreach (array_keys($items) as $index) {
19+
$offset++;
20+
if ($index === ($itemIndex - 1)) {
21+
break;
22+
}
1123
}
12-
}
13-
$items = array_slice($items, 0, $offset, true) + $item + array_slice($items, $offset, null, true);
14-
};
24+
$items = array_slice($items, 0, $offset, true) + $item + array_slice($items, $offset, null, true);
25+
};
1526
foreach ($vendorItemGroups as $key => $vendorItemGroup) {
1627
$count = count($vendorItemGroup);
1728
if ($count === 0) {
1829
continue;
1930
}
31+
/** @psalm-var non-empty-array<int, string> $vendorItemGroup */
2032
if ($count === 1) {
2133
$insertItem($applicationItems, $vendorItemGroup);
2234
continue;

templates/_previous-exception.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
2-
/* @var $throwable \Throwable */
3-
/* @var $this \Yiisoft\ErrorHandler\Renderer\HtmlRenderer */
2+
3+
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;
4+
5+
/**
6+
* @var Throwable $throwable
7+
* @var HtmlRenderer $this
8+
*/
49
?>
510
<div class="previous">
611
<span class="arrow">&crarr;</span>

templates/development.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
88

99
/**
10-
* @var $this HtmlRenderer
11-
* @var $request ServerRequestInterface|null
12-
* @var $throwable Throwable
10+
* @var ServerRequestInterface|null $request
11+
* @var Throwable $throwable
1312
*/
1413

1514
$theme = $_COOKIE['yii-exception-theme'] ?? '';
@@ -18,11 +17,13 @@
1817
if ($throwable instanceof CompositeException) {
1918
$throwable = $throwable->getFirstException();
2019
}
21-
$isFriendlyException = $throwable instanceof FriendlyExceptionInterface;
22-
$solution = $isFriendlyException ? $throwable->getSolution() : null;
20+
$solution = $throwable instanceof FriendlyExceptionInterface ? $throwable->getSolution() : null;
2321
$exceptionClass = get_class($throwable);
2422
$exceptionMessage = $throwable->getMessage();
2523

24+
/**
25+
* @var HtmlRenderer $this
26+
*/
2627
?>
2728
<!doctype html>
2829
<html lang="en">
@@ -78,7 +79,7 @@
7879
<div class="exception-card">
7980
<div class="exception-class">
8081
<?php
81-
if ($isFriendlyException): ?>
82+
if ($throwable instanceof FriendlyExceptionInterface): ?>
8283
<span><?= $this->htmlEncode($throwable->getName())?></span>
8384
&mdash;
8485
<?= $exceptionClass ?>
@@ -98,12 +99,12 @@
9899

99100
<?= $this->renderPreviousExceptions($originalException) ?>
100101

101-
<textarea id="clipboard"><?= $this->htmlEncode($throwable) ?></textarea>
102+
<textarea id="clipboard"><?= $this->htmlEncode((string) $throwable) ?></textarea>
102103
<span id="copied">Copied!</span>
103104

104105
<a href="#"
105106
class="copy-clipboard"
106-
data-clipboard="<?= $this->htmlEncode($throwable) ?>"
107+
data-clipboard="<?= $this->htmlEncode((string) $throwable) ?>"
107108
title="Copy the stacktrace for use in a bug report or pastebin"
108109
>
109110
<svg width="26" height="30" fill="none" xmlns="http://www.w3.org/2000/svg">

templates/production.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
2-
/* @var $throwable \Throwable */
3-
/* @var $this \Yiisoft\ErrorHandler\Renderer\HtmlRenderer */
42

53
use Yiisoft\ErrorHandler\Exception\UserException;
4+
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;
65
use Yiisoft\ErrorHandler\ThrowableRendererInterface;
76

7+
/**
8+
* @var Throwable $throwable
9+
* @var HtmlRenderer $this
10+
*/
11+
812
if ($throwable instanceof UserException) {
913
$name = $this->getThrowableName($throwable);
1014
$message = $throwable->getMessage();

0 commit comments

Comments
 (0)