Skip to content
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
2 changes: 2 additions & 0 deletions src/Enum/SymfonyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ final class SymfonyClass
public const string CONTAINER_CONFIGURATOR = 'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator';

public const string IS_GRANTED = 'Symfony\Component\Security\Http\Attribute\IsGranted';

public const string MAILER_TRANSPORT = 'Symfony\Component\Mailer\Transport';
}
10 changes: 10 additions & 0 deletions src/Rules/Symfony/PreferInterfaceInConstructorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\RuleIdentifier;
use Symplify\PHPStanRules\Enum\SymfonyClass;

/**
* A constructor dependency must be autowired by its interface, not by the concrete class.
Expand All @@ -38,6 +39,11 @@
*/
private const array HANDLED_NAMESPACE_PREFIXES = ['Symfony\\', 'Doctrine\\'];

/**
* @var string[]
*/
private const array SKIPPED_CLASSES = [SymfonyClass::MAILER_TRANSPORT];

public function __construct(
private ReflectionProvider $reflectionProvider,
) {
Expand Down Expand Up @@ -96,6 +102,10 @@ private function matchImplementedSameNamedInterface(string $className): ?string
return null;
}

if (in_array($className, self::SKIPPED_CLASSES, true)) {
return null;
}

if (! $this->reflectionProvider->hasClass($className)) {
return null;
}
Expand Down
11 changes: 11 additions & 0 deletions stubs/Symfony/Component/Mailer/Transport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\Mailer;

if (class_exists('Symfony\Component\Mailer\Transport')) {
return;
}

final class Transport implements TransportInterface
{
}
11 changes: 11 additions & 0 deletions stubs/Symfony/Component/Mailer/TransportInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\Mailer;

if (interface_exists('Symfony\Component\Mailer\TransportInterface')) {
return;
}

interface TransportInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Symfony\PreferInterfaceInConstructorRule\Fixture;

use Symfony\Component\Mailer\Transport;

final class SkipMailerTransport
{
public function __construct(
private Transport $transport,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static function provideData(): Iterator

yield [__DIR__ . '/Fixture/SkipRouterInterface.php', []];
yield [__DIR__ . '/Fixture/SkipProjectClass.php', []];
yield [__DIR__ . '/Fixture/SkipMailerTransport.php', []];
}

/**
Expand Down
Loading