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

Psalm 1 #267

Merged
merged 31 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
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: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down
15 changes: 10 additions & 5 deletions src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
use Yiisoft\Yii\Debug\Storage\StorageInterface;
use Yiisoft\Yii\Http\Event\BeforeRequest;

/**
* @psalm-type BacktraceType = list<array{file?:string,line?:int,function?:string,class?:class-string,object?:object,type?:string,args?:array}>
*/
final class Debugger
{
private bool $skipCollect = false;
private bool $active = false;

/**
* @param CollectorInterface[] $collectors
* @param string[] $ignoredRequests
* @param string[] $ignoredCommands
*/
public function __construct(
private readonly DebuggerIdGenerator $idGenerator,
private readonly StorageInterface $target,
/**
* @var CollectorInterface[]
*/
private readonly array $collectors,
private array $ignoredRequests = [],
private array $ignoredCommands = [],
Expand Down Expand Up @@ -117,7 +122,7 @@ private function isCommandIgnored(?string $command): bool
}

/**
* @param array $ignoredRequests Patterns for ignored request URLs.
* @param string[] $ignoredRequests Patterns for ignored request URLs.
*
* @see WildcardPattern
*/
Expand All @@ -129,7 +134,7 @@ public function withIgnoredRequests(array $ignoredRequests): self
}

/**
* @param array $ignoredCommands Patterns for ignored commands names.
* @param string[] $ignoredCommands Patterns for ignored commands names.
*
* @see WildcardPattern
*/
Expand Down
7 changes: 6 additions & 1 deletion src/FlattenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* Implements the Throwable interface
* Basically, this class removes all objects from the trace.
* Ported from Symfony components @link https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Debug/Exception/FlattenException.php
*
* @psalm-import-type BacktraceType from Debugger
*/
final class FlattenException implements Stringable
{
Expand Down Expand Up @@ -163,6 +165,8 @@ public function getTrace(): array

/**
* @param array $trace the Exception stack trace as an array.
*
* @psalm-param BacktraceType $trace
*/
private function setTrace(array $trace): void
{
Expand Down Expand Up @@ -293,12 +297,13 @@ private function flattenArgs(array $args, int $level = 0, int &$count = 0): arra
}

/**
* @return string the real class name of an incomplete class
* @return string The real class name of an incomplete class
*/
private function getClassNameFromIncomplete(__PHP_Incomplete_Class $value): string
{
$array = new ArrayObject($value);

/** @var string */
return $array['__PHP_Incomplete_Class_Name'];
}
}
15 changes: 14 additions & 1 deletion src/Helper/BacktraceIgnoreMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,44 @@
namespace Yiisoft\Yii\Debug\Helper;

use Yiisoft\Strings\CombinedRegexp;
use Yiisoft\Yii\Debug\Debugger;

/**
* All backtrace parameters should contain at least 4 elements in the following order:
* 0 – Called method
* 1 – Proxy
* 2 – Real using place / Composer\ClassLoader include function
* 3 – Whatever / Composer\ClassLoader
*
* @psalm-import-type BacktraceType from Debugger
*/
final class BacktraceIgnoreMatcher
{
/**
* @param string[] $patterns
* @psalm-param BacktraceType $backtrace
*/
public static function isIgnoredByFile(array $backtrace, array $patterns): bool
{
if (!isset($backtrace[2])) {
if (!isset($backtrace[2]['file'])) {
return false;
}
$path = $backtrace[2]['file'];

return self::doesStringMatchPattern($path, $patterns);
}

/**
* @psalm-param BacktraceType $backtrace
*/
public static function isIgnoredByClass(array $backtrace, array $classes): bool
{
return isset($backtrace[3]['class']) && in_array($backtrace[3]['class'], $classes, true);
}

/**
* @param string[] $patterns
*/
public static function doesStringMatchPattern(string $string, array $patterns): bool
{
if (empty($patterns)) {
Expand Down
1 change: 1 addition & 0 deletions src/Helper/StreamWrapper/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public function url_stat(string $path, int $flags): array|false

public function stream_metadata(string $path, int $option, mixed $value): bool
{
/** @psalm-suppress MixedArgument */
return match ($option) {
STREAM_META_TOUCH => touch($path, ...$value),
STREAM_META_OWNER_NAME, STREAM_META_OWNER => chown($path, $value),
Expand Down
38 changes: 27 additions & 11 deletions src/Storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use function glob;
use function strlen;
use function substr;
use function uasort;

final class FileStorage implements StorageInterface
{
Expand Down Expand Up @@ -49,16 +48,17 @@ public function setHistorySize(int $historySize): void
public function read(string $type, ?string $id = null): array
{
clearstatcache();
$data = [];
$pattern = sprintf(
'%s/**/%s/%s.json',
$this->path,
$id ?? '**',
$type,

$dataFiles = $this->findFilesOrderByModifiedTime(
vjik marked this conversation as resolved.
Show resolved Hide resolved
sprintf(
'%s/**/%s/%s.json',
$this->path,
$id ?? '**',
$type,
)
);
$dataFiles = glob($pattern, GLOB_NOSORT);
uasort($dataFiles, static fn ($a, $b) => filemtime($a) <=> filemtime($b));

$data = [];
foreach ($dataFiles as $file) {
$dir = dirname($file);
$id = substr($dir, strlen(dirname($file, 2)) + 1);
Expand Down Expand Up @@ -121,12 +121,11 @@ private function collectSummaryData(): array
*/
private function gc(): void
{
$summaryFiles = glob($this->path . '/**/**/summary.json', GLOB_NOSORT);
$summaryFiles = $this->findFilesOrderByModifiedTime($this->path . '/**/**/summary.json');
if (empty($summaryFiles) || count($summaryFiles) <= $this->historySize) {
return;
}

uasort($summaryFiles, static fn ($a, $b) => filemtime($b) <=> filemtime($a));
$excessFiles = array_slice($summaryFiles, $this->historySize);
foreach ($excessFiles as $file) {
$path1 = dirname($file);
Expand All @@ -143,4 +142,21 @@ private function gc(): void
}
}
}

/**
* @return string[]
*/
private function findFilesOrderByModifiedTime(string $pattern): array
{
$files = glob($pattern, GLOB_NOSORT);
if ($files === false) {
return [];
}

usort(
xepozz marked this conversation as resolved.
Show resolved Hide resolved
$files,
static fn (string $a, string $b) => filemtime($b) <=> filemtime($a)
);
return $files;
}
}
2 changes: 1 addition & 1 deletion src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function addCollector(CollectorInterface $collector): void;
/**
* Returns collected data from collectors added
*
* @return array collected data
* @return array[] The collected data
*/
public function getData(): array;

Expand Down
Loading