Skip to content

Commit

Permalink
Add longScanWarning config
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Feb 17, 2025
1 parent 0d749d7 commit 817ea4e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
3 changes: 2 additions & 1 deletion config.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
<xs:attribute name="cacheDirectory" type="xs:string" />
<xs:attribute name="errorBaseline" type="xs:string" />
<xs:attribute name="maxStringLength" type="xs:string" />
<xs:attribute name="maxShapedArraySize" type="xs:string" default="100" />
<xs:attribute name="maxShapedArraySize" type="xs:integer" default="100" />
<xs:attribute name="longScanWarning" type="xs:float" default="10.0" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="phpVersion" type="xs:string" />
<xs:attribute name="serializer" type="xs:string" />
Expand Down
11 changes: 11 additions & 0 deletions docs/running_psalm/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,17 @@ Arrays bigger than this value (100 by default) will be transformed in a generic

Please note that changing this setting might introduce unwanted side effects and those side effects won't be considered as bugs.

#### longScanWarning
```xml
<psalm
longScanWarning="10.0"
>
```

Specifies the maximum scan and analysis duration for individual files; files which take longer than the specified amount of seconds (as a floating point, so values smaller than one second are also accepted) will print out a warning to CLI (the scan will **not** be interrupted).

Only used in multithreaded mode, defaults to 10 seconds.

#### restrictReturnTypes

```xml
Expand Down
7 changes: 7 additions & 0 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ final class Config

public int $max_shaped_array_size = 100;

public float $long_scan_warning = 10.0;

/**
* @var string[]
*/
Expand Down Expand Up @@ -1171,6 +1173,11 @@ private static function fromXmlAndPaths(
$config->max_shaped_array_size = $attribute_text;
}

if (isset($config_xml['longScanWarning'])) {
$attribute_text = (float)$config_xml['longScanWarning'];
$config->long_scan_warning = $attribute_text;
}

if (isset($config_xml['inferPropertyTypesFromConstructor'])) {
$attribute_text = (string) $config_xml['inferPropertyTypesFromConstructor'];
$config->infer_property_types_from_constructor = $attribute_text === 'true' || $attribute_text === '1';
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Codebase/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ private function doAnalysis(ProjectAnalyzer $project_analyzer, int $pool_size):
// files up among a given number of child processes.
$pool = new Pool(
$pool_size,
$codebase->config->long_scan_warning,
$project_analyzer->progress,
);

Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Codebase/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ private function scanFilePaths(int $pool_size): bool
// files up among a given number of child processes.
$pool = new Pool(
$pool_size,
$this->config->long_scan_warning,
$this->progress,
);

Expand Down
15 changes: 9 additions & 6 deletions src/Psalm/Internal/Fork/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public function __sleep(): array
/**
* @param int<2, max> $threads
*/
public function __construct(public readonly int $threads, private readonly Progress $progress)
{
public function __construct(
public readonly int $threads,
private readonly float $timeLimit,
private readonly Progress $progress,
) {
$this->pool = new ContextWorkerPool(
$threads,
new ContextWorkerFactory(
Expand Down Expand Up @@ -97,11 +100,11 @@ public function run(
if ($task_done_closure) {
$f->map($task_done_closure);
}
$id = EventLoop::repeat(10.0, function () use ($file): void {
static $seconds = 10;
$this->progress->write(PHP_EOL."Processing $file is taking $seconds seconds...".PHP_EOL);
$id = EventLoop::repeat($this->timeLimit, function () use ($file): void {
static $seconds = 0.0;
/** @psalm-suppress MixedAssignment, MixedOperand */
$seconds += 10;
$seconds += $this->timeLimit;
$this->progress->write(PHP_EOL."Processing $file is taking $seconds seconds...".PHP_EOL);
});
$f->finally(static function () use ($id): void {
EventLoop::cancel($id);
Expand Down

0 comments on commit 817ea4e

Please sign in to comment.