Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase static analysis #37

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"require": {
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^2.0",
"thecodingmachine/safe": "^1.0 || ^2.0"
"thecodingmachine/safe": "^1.0 || ^2.0",
"nikic/php-parser": "^5"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
Expand Down
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
43 changes: 0 additions & 43 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.