Skip to content

Commit

Permalink
tests: use namespaces in code samples (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Aug 20, 2022
1 parent 365e87c commit 1053dae
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 27 deletions.
15 changes: 10 additions & 5 deletions tests/Rule/data/AllowComparingOnlyComparableTypesRule/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php declare(strict_types = 1);

namespace AllowComparingOnlyComparableTypesRule;

use DateTime;
use DateTimeImmutable;

interface Foo {}
interface Bar {}

Expand All @@ -21,20 +26,20 @@ interface Bar {}
float $float,
bool $bool,
) {
$foos > $foo; // error: Comparison array > Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$foos > $foo; // error: Comparison array > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$nullableInt > $int; // error: Comparison int|null > int contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
null > $int; // error: Comparison null > int contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$foo > $foo2; // error: Comparison Foo > Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$foo > $fooOrBar; // error: Comparison Foo > Bar|Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$foo > $fooAndBar; // error: Comparison Foo > Bar&Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$foo > $foo2; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$foo > $fooOrBar; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Bar|AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$foo > $fooAndBar; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Bar&AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$string > 'foo';
$int > 2;
$float > 2;
$int > $intOrFloat;
$string > $intOrFloat; // error: Cannot compare different types in string > float|int.
$bool > true; // error: Comparison bool > true contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$dateTime > $dateTimeImmutable;
$dateTime > $foo; // error: Comparison DateTime > Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
$dateTime > $foo; // error: Comparison DateTime > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.

$string > $int; // error: Cannot compare different types in string > int.
$float > $int;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php declare(strict_types = 1);

namespace AllowNamedArgumentOnlyInAttributesRule;

#[\Attribute(flags: \Attribute::TARGET_ALL)]
use Attribute;

#[Attribute(flags: Attribute::TARGET_ALL)]
class UniversalAttribute
{
public function __construct(int $argument)
Expand Down
6 changes: 5 additions & 1 deletion tests/Rule/data/BackedEnumGenericsRule/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace BackedEnumGenericsRule;

use BackedEnum;

/**
* @implements BackedEnum<string>
*/
Expand All @@ -19,7 +23,7 @@ interface MyBackedEnum extends BackedEnum {

}

enum MyIntEnumWithoutImplements: int { // error: Class MyIntEnumWithoutImplements extends generic BackedEnum, but does not specify its type. Use @implements BackedEnum<int>
enum MyIntEnumWithoutImplements: int { // error: Class BackedEnumGenericsRule\MyIntEnumWithoutImplements extends generic BackedEnum, but does not specify its type. Use @implements BackedEnum<int>
}

enum MyIntEnumWithImplementsInParent: int implements MyBackedEnum {
Expand Down
2 changes: 2 additions & 0 deletions tests/Rule/data/ForbidEnumInFunctionArgumentsRule/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php declare(strict_types = 1);

namespace ForbidEnumInFunctionArgumentsRule;

enum SomeEnum: string {
case Bar = 'bar';
case Baz = 'baz';
Expand Down
4 changes: 4 additions & 0 deletions tests/Rule/data/ForbidFetchOnMixedRuleTest/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace ForbidFetchOnMixedRuleTest;

use ReflectionClass;

$fn = function (mixed $mixed, $unknown, array $array, ReflectionClass $reflection) {
$mixed->fetch1; // error: Property fetch ->fetch1 is prohibited on unknown type ($mixed)
$unknown->fetch2; // error: Property fetch ->fetch2 is prohibited on unknown type ($unknown)
Expand Down
2 changes: 2 additions & 0 deletions tests/Rule/data/ForbidMatchDefaultArmForEnumsRule/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php declare(strict_types = 1);

namespace ForbidMatchDefaultArmForEnumsRule;

enum MyEnum: string {
case MyCase1 = 'MyCase1';
case MyCase2 = 'MyCase2';
Expand Down
4 changes: 4 additions & 0 deletions tests/Rule/data/ForbidMethodCallOnMixedRule/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace ForbidMethodCallOnMixedRule;

use ReflectionClass;

$fn = function (mixed $mixed, $unknown, array $array, ReflectionClass $reflection) {
$mixed->call1(); // error: Method call ->call1() is prohibited on unknown type ($mixed)
$unknown->call2(); // error: Method call ->call2() is prohibited on unknown type ($unknown)
Expand Down
2 changes: 2 additions & 0 deletions tests/Rule/data/ForbidReturnInConstructorRule/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php declare(strict_types = 1);

namespace ForbidReturnInConstructorRule;

class WithReturn {

public function __construct()
Expand Down
2 changes: 2 additions & 0 deletions tests/Rule/data/ForbidUnsetClassFieldRule/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ForbidUnsetClassFieldRule;

class Clazz {

public ?int $foo;
Expand Down
21 changes: 14 additions & 7 deletions tests/Rule/data/ForbidUnusedExceptionRule/code.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
<?php declare(strict_types = 1);

namespace ForbidUnusedExceptionRule;

use Exception;
use LogicException;
use RuntimeException;
use Throwable;

class ExampleClass
{

public function __construct()
{
$this->getExceptionAtRuntime(); // error: Method $this->getExceptionAtRuntime() returns exception that was not used in any way.
$this->getException(); // error: Method $this->getException() returns exception that was not used in any way.
new \Exception(); // error: Exception new \Exception() was not used in any way.
new Exception(); // error: Exception new \Exception() was not used in any way.

$this->okUsage1();
$this->okUsage2();
$this->okUsage3();
$this->okUsage4(new \LogicException());
$this->okUsage4(new LogicException());
}

public function okUsage1(): void
{
throw new \LogicException();
throw new LogicException();
}

public function okUsage2(): void
Expand All @@ -35,14 +42,14 @@ public function okUsage4(Throwable $throwable): void
$this->okUsage4($throwable);
}

public function getExceptionAtRuntime(): \RuntimeException
public function getExceptionAtRuntime(): RuntimeException
{
return new \RuntimeException();
return new RuntimeException();
}

public static function getException(): \RuntimeException
public static function getException(): RuntimeException
{
return new \RuntimeException();
return new RuntimeException();
}

}
2 changes: 2 additions & 0 deletions tests/Rule/data/ForbidUselessNullableReturnRule/code.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace ForbidUselessNullableReturnRule;

class ExampleClass {

private ?int $foo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

namespace RequirePreviousExceptionPassRule;

use Exception;
use LogicException;
use RuntimeException;
use Throwable;

class MyException extends RuntimeException {

public static function createForAnyPrevious(?Throwable $previous = null): self
Expand Down Expand Up @@ -35,12 +42,12 @@ public static function createWithoutPrevious(): self

try {
} catch (LogicException $e) {
throw MyException::createForAnyPrevious(); // error: Exception $e not passed as previous to \MyException::createForAnyPrevious()
throw MyException::createForAnyPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\MyException::createForAnyPrevious()
}

try {
} catch (RuntimeException $e) {
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \MyException::createForSpecificPrevious()
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\MyException::createForSpecificPrevious()
}

try {
Expand All @@ -61,5 +68,5 @@ public static function createWithoutPrevious(): self
try {
} catch (LogicException $e) {
} catch (LogicException|RuntimeException $e) { // Logic can never occur here, so Runtime must be passed
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \MyException::createForSpecificPrevious()
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\MyException::createForSpecificPrevious()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

namespace RequirePreviousExceptionPassRule;

use Exception;
use LogicException;
use RuntimeException;
use Throwable;

class MyException extends RuntimeException {

public static function createForAnyPrevious(?Throwable $previous = null): self
Expand Down Expand Up @@ -35,26 +42,26 @@ public static function createWithoutPrevious(): self

try {
} catch (LogicException $e) {
throw MyException::createForAnyPrevious(); // error: Exception $e not passed as previous to \MyException::createForAnyPrevious()
throw MyException::createForAnyPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\MyException::createForAnyPrevious()
}

try {
} catch (RuntimeException $e) {
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \MyException::createForSpecificPrevious()
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\MyException::createForSpecificPrevious()
}

try {
} catch (Throwable $e) {
throw MyException::createWithoutPrevious(); // error: Exception $e not passed as previous to \MyException::createWithoutPrevious()
throw MyException::createWithoutPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\MyException::createWithoutPrevious()
}

try {
} catch (LogicException $e) {
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \MyException::createForSpecificPrevious()
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\MyException::createForSpecificPrevious()
}

try {
} catch (LogicException|RuntimeException $e) {
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \MyException::createForSpecificPrevious()
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\MyException::createForSpecificPrevious()
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php declare(strict_types = 1);

class UselessPrivatePropertyDefaultValueRuleExampleClass
namespace UselessPrivatePropertyDefaultValueRule;

class ExampleClass
{
public int $isPublic = 0;

protected int $isProtected = 0;

private int $isPrivate = 0; // error: Property UselessPrivatePropertyDefaultValueRuleExampleClass::isPrivate has useless default value (overwritten in constructor)
private int $isPrivate = 0; // error: Property UselessPrivatePropertyDefaultValueRule\ExampleClass::isPrivate has useless default value (overwritten in constructor)

private int $isPrivateWithConditionalAssignment = 0; // not detected due to condition used in ctor

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php declare(strict_types = 1);

class UselessPrivatePropertyNullabilityRuleExampleClass
namespace UselessPrivatePropertyNullabilityRule;

class ExampleClass
{
public ?int $isPublic;

protected ?int $isProtected;

private ?int $isPrivate; // error: Property UselessPrivatePropertyNullabilityRuleExampleClass::isPrivate is defined as nullable, but null is never assigned
private ?int $isPrivate; // error: Property UselessPrivatePropertyNullabilityRule\ExampleClass::isPrivate is defined as nullable, but null is never assigned

private ?int $isPrivateAssigned;

private ?int $isPrivateWithConditionalAssignment;

private ?int $isPrivateWithDefaultNull = null;

private ?int $isPrivateWithDefaultNotNull = 1; // error: Property UselessPrivatePropertyNullabilityRuleExampleClass::isPrivateWithDefaultNotNull is defined as nullable, but null is never assigned
private ?int $isPrivateWithDefaultNotNull = 1; // error: Property UselessPrivatePropertyNullabilityRule\ExampleClass::isPrivateWithDefaultNotNull is defined as nullable, but null is never assigned

private ?int $isUninitialized;

Expand Down

0 comments on commit 1053dae

Please sign in to comment.