Skip to content

Commit

Permalink
Merge pull request #13 from alexraputa/psalm
Browse files Browse the repository at this point in the history
Closes #3: Add Psalm integration
  • Loading branch information
Ocramius authored Oct 11, 2021
2 parents 1f0e480 + 71627e0 commit 82aa871
Show file tree
Hide file tree
Showing 12 changed files with 2,738 additions and 1,010 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
},
"require-dev": {
"laminas/laminas-coding-standard": "~2.2.0",
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.4",
"psalm/plugin-phpunit": "^0.16.1",
"vimeo/psalm": "^4.8"
},
"suggest": {
"mezzio/mezzio-laminasviewrenderer": "^2.0 to use the laminas-view PhpRenderer template renderer",
Expand All @@ -50,6 +52,7 @@
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"static-analysis": "psalm --shepherd --stats",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
},
"replace": {
Expand Down
3,616 changes: 2,645 additions & 971 deletions composer.lock

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.8.1@f73f2299dbc59a3e6c4d66cff4605176e728ee69">
<file src="src/ArrayParametersTrait.php">
<MixedInferredReturnType occurrences="1">
<code>array</code>
</MixedInferredReturnType>
</file>
<file src="test/ExceptionTest.php">
<InvalidLiteralArgument occurrences="1">
<code>ExceptionInterface::class</code>
</InvalidLiteralArgument>
</file>
</files>
34 changes: 34 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
<directory name="test"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>

<issueHandlers>
<InternalMethod>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::method"/>
</errorLevel>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturn"/>
</errorLevel>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::with"/>
</errorLevel>
</InternalMethod>
</issueHandlers>

<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>
2 changes: 1 addition & 1 deletion src/DefaultParamsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
trait DefaultParamsTrait
{
/** @var array */
/** @var array<string, array<string, mixed>> */
private $defaultParams = [];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/TemplatePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TemplatePath
/** @var string */
protected $path;

/** @var null|string */
/** @var string|null */
protected $namespace;

public function __construct(string $path, ?string $namespace = null)
Expand Down
23 changes: 12 additions & 11 deletions test/ArrayParametersTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,39 @@ protected function setUp(): void
$this->subject = new TestAsset\ArrayParameters();
}

public function testNullParamsAreReturnedAsEmptyArray()
public function testNullParamsAreReturnedAsEmptyArray(): void
{
$this->assertEquals([], $this->subject->normalize(null));
self::assertEquals([], $this->subject->normalize(null));
}

public function testArrayParamsAreReturnedVerbatim()
public function testArrayParamsAreReturnedVerbatim(): void
{
$params = ['foo' => 'bar'];
$this->assertSame($params, $this->subject->normalize($params));
self::assertSame($params, $this->subject->normalize($params));
}

public function testExtractsVariablesFromObjectsImplementingGetVariables()
public function testExtractsVariablesFromObjectsImplementingGetVariables(): void
{
$params = ['foo' => 'bar'];
$model = new TestAsset\ViewModel($params);
$this->assertSame($params, $this->subject->normalize($model));
self::assertSame($params, $this->subject->normalize($model));
}

public function testCastsTraversablesToArrays()
public function testCastsTraversablesToArrays(): void
{
$params = ['foo' => 'bar'];
$model = new ArrayIterator($params);
$this->assertSame($params, $this->subject->normalize($model));
self::assertSame($params, $this->subject->normalize($model));
}

public function testCastsObjectsToArrays()
public function testCastsObjectsToArrays(): void
{
$params = ['foo' => 'bar'];
$model = (object) $params;
$this->assertSame($params, $this->subject->normalize($model));
self::assertSame($params, $this->subject->normalize($model));
}

/** @psalm-return array<string, array{scalar, string}> */
public function nonNullScalarParameters(): array
{
// @codingStandardsIgnoreStart
Expand All @@ -71,7 +72,7 @@ public function nonNullScalarParameters(): array
* @param mixed $scalar
* @param string $expectedString
*/
public function testNonNullScalarsRaiseAnException($scalar, $expectedString)
public function testNonNullScalarsRaiseAnException($scalar, $expectedString): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($expectedString);
Expand Down
12 changes: 6 additions & 6 deletions test/DefaultParamsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void
$this->defaultParams = new DefaultParameters();
}

public function testDefaultParamArray()
public function testDefaultParamArray(): void
{
$foo = [
1 => ['id' => 1],
Expand All @@ -39,10 +39,10 @@ public function testDefaultParamArray()
];

$this->defaultParams->addDefaultParam(TemplateRendererInterface::TEMPLATE_ALL, 'foo', $foo);
$this->assertEquals($expected, $this->defaultParams->getParameters());
self::assertEquals($expected, $this->defaultParams->getParameters());
}

public function testMergingParamsWithDefaultParamArray()
public function testMergingParamsWithDefaultParamArray(): void
{
$foo = [
1 => ['id' => 1],
Expand Down Expand Up @@ -74,17 +74,17 @@ public function testMergingParamsWithDefaultParamArray()
);
$params = $this->defaultParams->mergeParameters('template', $params);

$this->assertEquals($expected, $params);
self::assertEquals($expected, $params);
}

public function testExceptionOnAddDefaultParamWhenEmptyTemplateName()
public function testExceptionOnAddDefaultParamWhenEmptyTemplateName(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$templateName must be a non-empty string');
$this->defaultParams->addDefaultParam('', 'name', 'value');
}

public function testExceptionOnAddDefaultParamWhenEmptyParamName()
public function testExceptionOnAddDefaultParamWhenEmptyParamName(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$param must be a non-empty string');
Expand Down
3 changes: 2 additions & 1 deletion test/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

class ExceptionTest extends TestCase
{
/** @psalm-return Generator<string, array<int, string>> */
public function exception(): Generator
{
$namespace = substr(ExceptionInterface::class, 0, strrpos(ExceptionInterface::class, '\\') + 1);
$namespace = substr(ExceptionInterface::class, 0, (int) strrpos(ExceptionInterface::class, '\\') + 1);

$exceptions = glob(__DIR__ . '/../src/Exception/*.php');
foreach ($exceptions as $exception) {
Expand Down
29 changes: 16 additions & 13 deletions test/TemplatePathAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,46 @@
trait TemplatePathAssertionsTrait
{
/** @param mixed $path */
public function assertTemplatePath($path, TemplatePath $templatePath, ?string $message = null)
public function assertTemplatePath($path, TemplatePath $templatePath, ?string $message = null): void
{
$message = $message ?: sprintf('Failed to assert TemplatePath contained path %s', $path);
$this->assertEquals($path, $templatePath->getPath(), $message);
$message = $message ?: sprintf('Failed to assert TemplatePath contained path %s', (string) $path);
self::assertEquals($path, $templatePath->getPath(), $message);
}

/** @param mixed $path */
public function assertTemplatePathString($path, TemplatePath $templatePath, ?string $message = null)
public function assertTemplatePathString($path, TemplatePath $templatePath, ?string $message = null): void
{
$message = $message ?: sprintf('Failed to assert TemplatePath casts to string path %s', $path);
$this->assertEquals($path, (string) $templatePath, $message);
$message = $message ?: sprintf('Failed to assert TemplatePath casts to string path %s', (string) $path);
self::assertEquals($path, (string) $templatePath, $message);
}

/** @param mixed $namespace */
public function assertTemplatePathNamespace($namespace, TemplatePath $templatePath, ?string $message = null)
public function assertTemplatePathNamespace($namespace, TemplatePath $templatePath, ?string $message = null): void
{
$message = $message ?: sprintf(
'Failed to assert TemplatePath namespace matched %s',
var_export($namespace, true)
);
$this->assertEquals($namespace, $templatePath->getNamespace(), $message);
self::assertEquals($namespace, $templatePath->getNamespace(), $message);
}

public function assertEmptyTemplatePathNamespace(TemplatePath $templatePath, ?string $message = null)
public function assertEmptyTemplatePathNamespace(TemplatePath $templatePath, ?string $message = null): void
{
$message = $message ?: 'Failed to assert TemplatePath namespace was empty';
$this->assertEmpty($templatePath->getNamespace(), $message);
self::assertEmpty($templatePath->getNamespace(), $message);
}

public function assertEqualTemplatePath(TemplatePath $expected, TemplatePath $received, ?string $message = null)
{
public function assertEqualTemplatePath(
TemplatePath $expected,
TemplatePath $received,
?string $message = null
): void {
$message = $message ?: 'Failed to assert TemplatePaths are equal';
if (
$expected->getPath() !== $received->getPath()
|| $expected->getNamespace() !== $received->getNamespace()
) {
$this->fail($message);
self::fail($message);
}
}
}
6 changes: 3 additions & 3 deletions test/TemplatePathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ class TemplatePathTest extends TestCase
{
use TemplatePathAssertionsTrait;

public function testCanProvideNamespaceAtInstantiation()
public function testCanProvideNamespaceAtInstantiation(): void
{
$templatePath = new TemplatePath('/tmp', 'test');
$this->assertTemplatePath('/tmp', $templatePath);
$this->assertTemplatePathNamespace('test', $templatePath);
}

public function testCanInstantiateWithoutANamespace()
public function testCanInstantiateWithoutANamespace(): void
{
$templatePath = new TemplatePath('/tmp');
$this->assertTemplatePath('/tmp', $templatePath);
$this->assertEmptyTemplatePathNamespace($templatePath);
}

public function testCastingToStringReturnsPathOnly()
public function testCastingToStringReturnsPathOnly(): void
{
$templatePath = new TemplatePath('/tmp');
$this->assertTemplatePathString('/tmp', $templatePath);
Expand Down
3 changes: 1 addition & 2 deletions test/TestAsset/DefaultParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public function getParameters(): array
return $this->defaultParams;
}

/** @param mixed $template */
public function mergeParameters($template, array $params): array
public function mergeParameters(string $template, array $params): array
{
return $this->mergeParams($template, $params);
}
Expand Down

0 comments on commit 82aa871

Please sign in to comment.