Skip to content

Commit a71a20b

Browse files
holymp2006cmgmyr
andauthored
Add option to set timeout (#595)
* fix: Add timeout option to config * fix: Add timeout option to laravel config * fix: Add timeout option to all config stubs * change to int * formatting * sync config --------- Co-authored-by: Chris Gmyr <[email protected]>
1 parent 8243bb8 commit a71a20b

File tree

9 files changed

+125
-15
lines changed

9 files changed

+125
-15
lines changed

src/Domain/Configuration.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ final class Configuration
108108

109109
private int $diffContext;
110110

111+
private int $timeout;
112+
111113
/**
112114
* Configuration constructor.
113115
*
@@ -244,6 +246,11 @@ public function getDiffContext(): int
244246
return $this->diffContext;
245247
}
246248

249+
public function getTimeout(): int
250+
{
251+
return $this->timeout;
252+
}
253+
247254
/**
248255
* @param array<string, string|int|array|null> $config
249256
*/
@@ -265,6 +272,7 @@ private function resolveConfig(array $config): void
265272

266273
$resolver->setDefined('ide');
267274
$resolver->setDefined('threads');
275+
$resolver->setDefined('timeout');
268276
$resolver->setAllowedValues(
269277
'preset',
270278
array_map(static fn (string $presetClass) => $presetClass::getName(), self::PRESETS)
@@ -277,6 +285,7 @@ private function resolveConfig(array $config): void
277285
$resolver->setAllowedTypes('diff_context', 'int');
278286
$resolver->setAllowedValues('diff_context', static fn ($value) => $value >= 0);
279287
$resolver->setAllowedValues('threads', static fn ($value) => $value === null || $value >= 1);
288+
$resolver->setAllowedValues('timeout', static fn ($value) => $value >= 0);
280289

281290
try {
282291
$config = $resolver->resolve($config);
@@ -302,20 +311,23 @@ private function resolveConfig(array $config): void
302311
$this->fix = $config['fix'];
303312
$this->diffContext = $config['diff_context'];
304313

305-
if (array_key_exists('ide', $config)
314+
if (
315+
array_key_exists('ide', $config)
306316
&& is_string($config['ide'])
307317
&& $config['ide'] !== ''
308318
) {
309319
$this->fileLinkFormatter = $this->resolveIde($config['ide']);
310320
}
311321
$this->threads = $config['threads'] ?? $this->getNumberOfCore();
322+
$this->timeout = $config['timeout'] ?? 60;
312323
}
313324

314325
private function validateAddedInsight(): Closure
315326
{
316327
return static function ($values): bool {
317328
foreach ($values as $metric => $insights) {
318-
if (! class_exists($metric) ||
329+
if (
330+
! class_exists($metric) ||
319331
class_implements($metric) === false ||
320332
! in_array(Metric::class, class_implements($metric), true)
321333
) {
@@ -364,8 +376,10 @@ private function validateConfigInsights(): Closure
364376

365377
private function resolveIde(string $ide): FileLinkFormatterContract
366378
{
367-
if (! isset(self::LINKS[$ide]) &&
368-
mb_strpos($ide, '://') === false) {
379+
if (
380+
! isset(self::LINKS[$ide]) &&
381+
mb_strpos($ide, '://') === false
382+
) {
369383
throw new InvalidConfiguration(sprintf(
370384
'Unknown IDE "%s". Try one in this list [%s] or provide pattern link handler',
371385
$ide,

src/Domain/Insights/SyntaxCheck.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function process(): void
5151
if ($toAnalyse === '.' && getcwd() !== rtrim($this->collector->getCommonPath(), DIRECTORY_SEPARATOR)) {
5252
$process->setWorkingDirectory($this->collector->getCommonPath());
5353
}
54-
$process->run();
54+
$configuration = Container::make()->get(Configuration::class);
55+
$process->setTimeout($configuration->getTimeout())->run();
5556

5657
$output = json_decode($process->getOutput(), true, 512, JSON_THROW_ON_ERROR);
5758
$errors = $output['results']['errors'] ?? [];

stubs/config.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| to make your code reliable, simple, and clean. However, you can always
1414
| adjust the `Metrics` and `Insights` below in this configuration file.
1515
|
16-
| Supported: "default", "laravel", "symfony", "magento2", "drupal"
16+
| Supported: "default", "laravel", "symfony", "magento2", "drupal", "wordpress"
1717
|
1818
*/
1919

@@ -102,4 +102,16 @@
102102

103103
'threads' => null,
104104

105+
/*
106+
|--------------------------------------------------------------------------
107+
| Timeout
108+
|--------------------------------------------------------------------------
109+
| Here you may adjust the timeout (in seconds) for PHPInsights to run before
110+
| a ProcessTimedOutException is thrown.
111+
| This accepts an int > 0. Default is 60 seconds, which is the default value
112+
| of Symfony's setTimeout function.
113+
|
114+
*/
115+
116+
'timeout' => 60,
105117
];

stubs/drupal.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| to make your code reliable, simple, and clean. However, you can always
1414
| adjust the `Metrics` and `Insights` below in this configuration file.
1515
|
16-
| Supported: "default", "laravel", "symfony", "magento2", "drupal"
16+
| Supported: "default", "laravel", "symfony", "magento2", "drupal", "wordpress"
1717
|
1818
*/
1919

@@ -95,11 +95,23 @@
9595
|--------------------------------------------------------------------------
9696
|
9797
| Here you may adjust how many threads (core) PHPInsights can use to perform
98-
| the analyse. This is optional, don't provide it and the tool will guess
98+
| the analysis. This is optional, don't provide it and the tool will guess
9999
| the max core number available. It accepts null value or integer > 0.
100100
|
101101
*/
102102

103103
'threads' => null,
104104

105+
/*
106+
|--------------------------------------------------------------------------
107+
| Timeout
108+
|--------------------------------------------------------------------------
109+
| Here you may adjust the timeout (in seconds) for PHPInsights to run before
110+
| a ProcessTimedOutException is thrown.
111+
| This accepts an int > 0. Default is 60 seconds, which is the default value
112+
| of Symfony's setTimeout function.
113+
|
114+
*/
115+
116+
'timeout' => 60,
105117
];

stubs/laravel.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
| to make your code reliable, simple, and clean. However, you can always
2828
| adjust the `Metrics` and `Insights` below in this configuration file.
2929
|
30-
| Supported: "default", "laravel", "symfony", "magento2", "drupal"
30+
| Supported: "default", "laravel", "symfony", "magento2", "drupal", "wordpress"
3131
|
3232
*/
3333

@@ -60,7 +60,7 @@
6060
|
6161
| Here you may adjust all the various `Insights` that will be used by PHP
6262
| Insights. You can either add, remove or configure `Insights`. Keep in
63-
| mind that all added `Insights` must belong to a specific `Metric`.
63+
| mind, that all added `Insights` must belong to a specific `Metric`.
6464
|
6565
*/
6666

@@ -118,11 +118,23 @@
118118
|--------------------------------------------------------------------------
119119
|
120120
| Here you may adjust how many threads (core) PHPInsights can use to perform
121-
| the analyse. This is optional, don't provide it and the tool will guess
121+
| the analysis. This is optional, don't provide it and the tool will guess
122122
| the max core number available. It accepts null value or integer > 0.
123123
|
124124
*/
125125

126126
'threads' => null,
127127

128+
/*
129+
|--------------------------------------------------------------------------
130+
| Timeout
131+
|--------------------------------------------------------------------------
132+
| Here you may adjust the timeout (in seconds) for PHPInsights to run before
133+
| a ProcessTimedOutException is thrown.
134+
| This accepts an int > 0. Default is 60 seconds, which is the default value
135+
| of Symfony's setTimeout function.
136+
|
137+
*/
138+
139+
'timeout' => 60,
128140
];

stubs/magento2.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| to make your code reliable, simple, and clean. However, you can always
1414
| adjust the `Metrics` and `Insights` below in this configuration file.
1515
|
16-
| Supported: "default", "laravel", "symfony", "magento2", "drupal"
16+
| Supported: "default", "laravel", "symfony", "magento2", "drupal", "wordpress"
1717
|
1818
*/
1919

@@ -95,11 +95,23 @@
9595
|--------------------------------------------------------------------------
9696
|
9797
| Here you may adjust how many threads (core) PHPInsights can use to perform
98-
| the analyse. This is optional, don't provide it and the tool will guess
98+
| the analysis. This is optional, don't provide it and the tool will guess
9999
| the max core number available. It accepts null value or integer > 0.
100100
|
101101
*/
102102

103103
'threads' => null,
104104

105+
/*
106+
|--------------------------------------------------------------------------
107+
| Timeout
108+
|--------------------------------------------------------------------------
109+
| Here you may adjust the timeout (in seconds) for PHPInsights to run before
110+
| a ProcessTimedOutException is thrown.
111+
| This accepts an int > 0. Default is 60 seconds, which is the default value
112+
| of Symfony's setTimeout function.
113+
|
114+
*/
115+
116+
'timeout' => 60,
105117
];

stubs/symfony.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| to make your code reliable, simple, and clean. However, you can always
1414
| adjust the `Metrics` and `Insights` below in this configuration file.
1515
|
16-
| Supported: "default", "laravel", "symfony", "magento2", "drupal"
16+
| Supported: "default", "laravel", "symfony", "magento2", "drupal", "wordpress"
1717
|
1818
*/
1919

@@ -95,11 +95,23 @@
9595
|--------------------------------------------------------------------------
9696
|
9797
| Here you may adjust how many threads (core) PHPInsights can use to perform
98-
| the analyse. This is optional, don't provide it and the tool will guess
98+
| the analysis. This is optional, don't provide it and the tool will guess
9999
| the max core number available. It accepts null value or integer > 0.
100100
|
101101
*/
102102

103103
'threads' => null,
104104

105+
/*
106+
|--------------------------------------------------------------------------
107+
| Timeout
108+
|--------------------------------------------------------------------------
109+
| Here you may adjust the timeout (in seconds) for PHPInsights to run before
110+
| a ProcessTimedOutException is thrown.
111+
| This accepts an int > 0. Default is 60 seconds, which is the default value
112+
| of Symfony's setTimeout function.
113+
|
114+
*/
115+
116+
'timeout' => 60,
105117
];

stubs/wordpress.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
'preset' => 'wordpress',
21+
2122
/*
2223
|--------------------------------------------------------------------------
2324
| IDE
@@ -37,6 +38,7 @@
3738
*/
3839

3940
'ide' => null,
41+
4042
/*
4143
|--------------------------------------------------------------------------
4244
| Configuration
@@ -87,4 +89,29 @@
8789
// 'disable-security-check' => false,
8890
],
8991

92+
/*
93+
|--------------------------------------------------------------------------
94+
| Threads
95+
|--------------------------------------------------------------------------
96+
|
97+
| Here you may adjust how many threads (core) PHPInsights can use to perform
98+
| the analysis. This is optional, don't provide it and the tool will guess
99+
| the max core number available. It accepts null value or integer > 0.
100+
|
101+
*/
102+
103+
'threads' => null,
104+
105+
/*
106+
|--------------------------------------------------------------------------
107+
| Timeout
108+
|--------------------------------------------------------------------------
109+
| Here you may adjust the timeout (in seconds) for PHPInsights to run before
110+
| a ProcessTimedOutException is thrown.
111+
| This accepts an int > 0. Default is 60 seconds, which is the default value
112+
| of Symfony's setTimeout function.
113+
|
114+
*/
115+
116+
'timeout' => 60,
90117
];

tests/Domain/ConfigurationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,12 @@ public function invalidThreadsNumber(): array
137137
['2'],
138138
];
139139
}
140+
141+
public function testDefineTimeout(): void
142+
{
143+
$expected = random_int(60, 120);
144+
$configuration = new Configuration(['timeout' => $expected]);
145+
146+
self::assertSame($expected, $configuration->getTimeout());
147+
}
140148
}

0 commit comments

Comments
 (0)