Skip to content

Commit dc43964

Browse files
committed
refactor: extract runPhpStan() to PhpStanTestTrait
1 parent cfb0f6b commit dc43964

3 files changed

Lines changed: 42 additions & 42 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright 2025, Cake Development Corporation (https://www.cakedc.com)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright 2025, Cake Development Corporation (https://www.cakedc.com)
11+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12+
*/
13+
14+
namespace CakeDC\PHPStan\Test\TestCase;
15+
16+
trait PhpStanTestTrait
17+
{
18+
/**
19+
* Run PHPStan on a file and return the output.
20+
*
21+
* @param string $file File to analyze
22+
* @return string
23+
*/
24+
private function runPhpStan(string $file): string
25+
{
26+
$configFile = dirname(__DIR__, 2) . '/extension.neon';
27+
$command = sprintf(
28+
'cd %s && vendor/bin/phpstan analyze %s --level=max --configuration=%s --no-progress 2>&1',
29+
escapeshellarg(dirname(__DIR__, 2)),
30+
escapeshellarg($file),
31+
escapeshellarg($configFile),
32+
);
33+
34+
exec($command, $output, $exitCode);
35+
36+
return implode("\n", $output);
37+
}
38+
}

tests/TestCase/Type/SelectQueryFindListReturnTypeExtensionTest.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
namespace CakeDC\PHPStan\Test\TestCase\Type;
1515

16+
use CakeDC\PHPStan\Test\TestCase\PhpStanTestTrait;
1617
use PHPUnit\Framework\TestCase;
1718

1819
class SelectQueryFindListReturnTypeExtensionTest extends TestCase
1920
{
21+
use PhpStanTestTrait;
2022
/**
2123
* Test that find('list')->toArray() returns correct type.
2224
*
@@ -49,25 +51,4 @@ public function testFindListWithGroupFieldReturnsNestedArray(): void
4951
$output = $this->runPhpStan(__DIR__ . '/Fake/FindListGroupedUsage.php');
5052
static::assertStringContainsString('[OK] No errors', $output);
5153
}
52-
53-
/**
54-
* Run PHPStan on a file and return the output.
55-
*
56-
* @param string $file File to analyze
57-
* @return string
58-
*/
59-
private function runPhpStan(string $file): string
60-
{
61-
$configFile = dirname(__DIR__, 3) . '/extension.neon';
62-
$command = sprintf(
63-
'cd %s && vendor/bin/phpstan analyze %s --level=max --configuration=%s --no-progress 2>&1',
64-
escapeshellarg(dirname(__DIR__, 3)),
65-
escapeshellarg($file),
66-
escapeshellarg($configFile),
67-
);
68-
69-
exec($command, $output, $exitCode);
70-
71-
return implode("\n", $output);
72-
}
7354
}

tests/TestCase/Type/TypeFactoryBuildDynamicReturnTypeExtensionTest.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
namespace CakeDC\PHPStan\Test\TestCase\Type;
1515

16+
use CakeDC\PHPStan\Test\TestCase\PhpStanTestTrait;
1617
use PHPUnit\Framework\TestCase;
1718

1819
class TypeFactoryBuildDynamicReturnTypeExtensionTest extends TestCase
1920
{
21+
use PhpStanTestTrait;
2022
/**
2123
* Test that TypeFactory::build() returns correct types and allows valid method calls.
2224
*
@@ -43,25 +45,4 @@ public function testTypeFactoryBuildCatchesInvalidMethodCalls(): void
4345
static::assertStringContainsString('JsonType::nonExistentMethod()', $output);
4446
static::assertStringContainsString('Found 4 errors', $output);
4547
}
46-
47-
/**
48-
* Run PHPStan on a file and return the output.
49-
*
50-
* @param string $file File to analyze
51-
* @return string
52-
*/
53-
private function runPhpStan(string $file): string
54-
{
55-
$configFile = dirname(__DIR__, 3) . '/extension.neon';
56-
$command = sprintf(
57-
'cd %s && vendor/bin/phpstan analyze %s --level=max --configuration=%s --no-progress 2>&1',
58-
escapeshellarg(dirname(__DIR__, 3)),
59-
escapeshellarg($file),
60-
escapeshellarg($configFile),
61-
);
62-
63-
exec($command, $output, $exitCode);
64-
65-
return implode("\n", $output);
66-
}
6748
}

0 commit comments

Comments
 (0)