Skip to content

Commit

Permalink
Merge pull request #37 from Jean85/increase-static-analysis
Browse files Browse the repository at this point in the history
Increase static analysis
  • Loading branch information
dbrekelmans authored Nov 28, 2024
2 parents 150031e + ca626cc commit 72030d0
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 121 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ parameters:
level: max
paths:
- src
- tests
excludePaths:
- tests/Rules/data
ignoreErrors:
-
message: '#^Implementing PHPStan\\Rules\\IdentifierRuleError is not covered by backward compatibility promise\. The interface might change in a minor PHPStan version\.$#'
Expand Down
5 changes: 4 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="tests/bootstrap.php"
bootstrap="vendor/autoload.php"
colors="true"
backupGlobals="false"
backupStaticAttributes="false"
Expand All @@ -24,6 +24,9 @@
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>src/Type/Php/ReplaceSafeFunctionsDynamicReturnTypeExtension.php</file>
</exclude>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ReplaceSafeFunctionsDynamicReturnTypeExtension implements DynamicFunctionR
{

/** @var array<string, int> */
private $functions = [
private array $functions = [
'Safe\preg_replace' => 2,
];

Expand Down
60 changes: 0 additions & 60 deletions tests/Rules/CallMethodRuleTest.php

This file was deleted.

9 changes: 6 additions & 3 deletions tests/Rules/UseSafeClassesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace TheCodingMachine\Safe\PHPStan\Rules;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use TheCodingMachine\Safe\PHPStan\Type\Php\ReplaceSafeFunctionsDynamicReturnTypeExtension;

/**
* @template-extends RuleTestCase<UseSafeClassesRule>
*/
class UseSafeClassesRuleTest extends RuleTestCase
{
protected function getRule(): \PHPStan\Rules\Rule
protected function getRule(): Rule
{
return new UseSafeClassesRule();
}

public function testDateTime()
public function testDateTime(): void
{
$this->analyse([__DIR__ . '/data/datetime.php'], [
[
Expand Down
29 changes: 12 additions & 17 deletions tests/Rules/UseSafeFunctionsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace TheCodingMachine\Safe\PHPStan\Rules;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use TheCodingMachine\Safe\PHPStan\Type\Php\ReplaceSafeFunctionsDynamicReturnTypeExtension;

/**
* @template-extends RuleTestCase<UseSafeFunctionsRule>
*/
class UseSafeFunctionsRuleTest extends RuleTestCase
{
protected function getRule(): \PHPStan\Rules\Rule
protected function getRule(): Rule
{
return new UseSafeFunctionsRule();
}

public function testCatch()
public function testCatch(): void
{
$this->analyse([__DIR__ . '/data/fopen.php'], [
[
Expand All @@ -22,31 +25,23 @@ public function testCatch()
]);
}

public function testNoCatchSafe()
public function testNoCatchSafe(): void
{
$this->analyse([__DIR__ . '/data/safe_fopen.php'], []);
}

public function testExprCall()
public function testExprCall(): void
{
$this->analyse([__DIR__ . '/data/undirect_call.php'], []);
}

public function testJSONDecodeNoCatchSafe()
public function testJSONDecodeNoCatchSafe(): void
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$this->analyse([__DIR__ . '/data/safe_json_decode_for_7.3.0.php'], []);
} else {
$this->assertTrue(true);
}
$this->analyse([__DIR__ . '/data/safe_json_decode_for_7.3.0.php'], []);
}

public function testJSONEncodeNoCatchSafe()
public function testJSONEncodeNoCatchSafe(): void
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$this->analyse([__DIR__ . '/data/safe_json_encode_for_7.3.0.php'], []);
} else {
$this->assertTrue(true);
}
$this->analyse([__DIR__ . '/data/safe_json_encode_for_7.3.0.php'], []);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace TheCodingMachine\Safe\PHPStan\Type\Php;

use PHPUnit\Framework\TestCase;

class ReplaceSafeFunctionsDynamicReturnTypeExtensionTest extends TestCase
{
public function testWithStrings(): void
{
$x = \Safe\preg_replace('/foo/', 'bar', 'baz');

$this->assertStringNotContainsString('foo', $x);
}

public function testWithArrays(): void
{
$x = \Safe\preg_replace(['/foo/'], ['bar'], ['baz']);

$this->assertNotContains('foo', $x);
}
}
4 changes: 2 additions & 2 deletions tests/Utils/FunctionListLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

class FunctionListLoaderTest extends TestCase
{

public function testGetFunctionList()
public function testGetFunctionList(): void
{
$functions = FunctionListLoader::getFunctionList();
$this->assertArrayHasKey('fopen', $functions);
$this->assertEquals('fopen', $functions['fopen']);
}
}
33 changes: 0 additions & 33 deletions tests/bootstrap.php

This file was deleted.

4 changes: 0 additions & 4 deletions tests/phpstan-bootstrap.php

This file was deleted.

0 comments on commit 72030d0

Please sign in to comment.