Skip to content
Closed
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
15 changes: 15 additions & 0 deletions config/set/throws-tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector;

/**
* Opt-in: the unchecked exceptions are project specific, so the rule has no default list.
* Enable it with ->withPreparedSets(throwsTag: true) and add yours with
* ->withConfiguredRule(AddThrowsTagRector::class, [LogicException::class]).
*/
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([AddThrowsTagRector::class]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddThrowsTagRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class BasicTransformation
{
public function run(): void
{
throw new \RuntimeException('boom');
}
}
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class BasicTransformation
{
/**
* @throws \RuntimeException
*/
public function run(): void
{
throw new \RuntimeException('boom');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class ExceptionNotCoveredByCatch
{
public function run(): void
{
try {
throw new \RuntimeException('boom');
} catch (\DomainException) {
}
}
}
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class ExceptionNotCoveredByCatch
{
/**
* @throws \RuntimeException
*/
public function run(): void
{
try {
throw new \RuntimeException('boom');
} catch (\DomainException) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class PropagatedFromCalledMethod
{
public function run(): void
{
$this->fail();
}

/**
* @throws \RuntimeException
*/
private function fail(): void
{
throw new \RuntimeException('boom');
}
}
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class PropagatedFromCalledMethod
{
/**
* @throws \RuntimeException
*/
public function run(): void
{
$this->fail();
}

/**
* @throws \RuntimeException
*/
private function fail(): void
{
throw new \RuntimeException('boom');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class ThrowingConstructor
{
/**
* @throws \RuntimeException
*/
public function __construct()
{
throw new \RuntimeException('boom');
}
}

final class PropagatedFromConstructor
{
public function run(): ThrowingConstructor
{
return new ThrowingConstructor();
}
}
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class ThrowingConstructor
{
/**
* @throws \RuntimeException
*/
public function __construct()
{
throw new \RuntimeException('boom');
}
}

final class PropagatedFromConstructor
{
/**
* @throws \RuntimeException
*/
public function run(): ThrowingConstructor
{
return new ThrowingConstructor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

interface CallerProvider
{
/**
* @throws \RuntimeException
*/
public function get(): int;
}

final class PropagatedFromInterfaceMethodOnProperty
{
public function __construct(
private CallerProvider $callerProvider,
) {}

public function run(): int
{
return $this->callerProvider->get();
}
}
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

interface CallerProvider
{
/**
* @throws \RuntimeException
*/
public function get(): int;
}

final class PropagatedFromInterfaceMethodOnProperty
{
public function __construct(
private CallerProvider $callerProvider,
) {}

/**
* @throws \RuntimeException
*/
public function run(): int
{
return $this->callerProvider->get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class StaticThrower
{
/**
* @throws \RuntimeException
*/
public static function fail(): void
{
throw new \RuntimeException('boom');
}
}

final class PropagatedFromStaticCall
{
public function run(): void
{
StaticThrower::fail();
}
}
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class StaticThrower
{
/**
* @throws \RuntimeException
*/
public static function fail(): void
{
throw new \RuntimeException('boom');
}
}

final class PropagatedFromStaticCall
{
/**
* @throws \RuntimeException
*/
public function run(): void
{
StaticThrower::fail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class RethrownFromCatchBlock
{
public function run(): void
{
try {
throw new \RuntimeException('boom');
} catch (\RuntimeException $runtimeException) {
throw $runtimeException;
}
}
}
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class RethrownFromCatchBlock
{
/**
* @throws \RuntimeException
*/
public function run(): void
{
try {
throw new \RuntimeException('boom');
} catch (\RuntimeException $runtimeException) {
throw $runtimeException;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class RethrownUnionFromCatchBlock
{
public function run(): void
{
try {
$this->work();
} catch (\RuntimeException | \JsonException $exception) {
throw $exception;
}
}

/**
* @throws \RuntimeException
* @throws \JsonException
*/
private function work(): void
{
throw new \RuntimeException('boom');
}
}
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddThrowsTagRector\Fixture;

final class RethrownUnionFromCatchBlock
{
/**
* @throws \RuntimeException
* @throws \JsonException
*/
public function run(): void
{
try {
$this->work();
} catch (\RuntimeException | \JsonException $exception) {
throw $exception;
}
}

/**
* @throws \RuntimeException
* @throws \JsonException
*/
private function work(): void
{
throw new \RuntimeException('boom');
}
}
Loading
Loading