Skip to content

Commit

Permalink
[CodeQuality] Add AddTraitGetterReturnTypeBasedOnSetterRequiredRector (
Browse files Browse the repository at this point in the history
…#671)

* [CodeQuality] Add AddTraitGetterReturnTypeBasedOnSetterRequiredRector

* [CodeQuality] Add AddTraitGetterReturnTypeBasedOnSetterRequiredRector

* Fix phpstan

* Add Required attribute support

* cs fix
  • Loading branch information
samsonasik authored Oct 3, 2024
1 parent 14fcc87 commit 928f117
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector;

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

final class AddTraitGetterReturnTypeBasedOnSetterRequiredRectorTest 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,51 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector\Fixture;

use stdClass;

trait FlipSetFirst
{
private $service;

/**
* @required
*/
public function setService(stdClass $stdClass)
{
$this->stdClass = $stdClass;
}

public function getService()
{
return $this->service;
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector\Fixture;

use stdClass;

trait FlipSetFirst
{
private $service;

/**
* @required
*/
public function setService(stdClass $stdClass)
{
$this->stdClass = $stdClass;
}

public function getService(): stdClass
{
return $this->service;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector\Fixture;

use stdClass;

trait SkipAlreadyHasReturnType
{
private $service;

public function getService(): stdClass
{
return $this->service;
}

/**
* @required
*/
public function setService(stdClass $stdClass)
{
$this->stdClass = $stdClass;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector\Fixture;

use stdClass;

trait SomeTrait
{
private $service;

public function getService()
{
return $this->service;
}

/**
* @required
*/
public function setService(stdClass $stdClass)
{
$this->stdClass = $stdClass;
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector\Fixture;

use stdClass;

trait SomeTrait
{
private $service;

public function getService(): stdClass
{
return $this->service;
}

/**
* @required
*/
public function setService(stdClass $stdClass)
{
$this->stdClass = $stdClass;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector\Fixture;

use stdClass;
use Symfony\Contracts\Service\Attribute\Required;

trait WithRequiredAttribute
{
private $service;

public function getService()
{
return $this->service;
}

#[Required]
public function setService(stdClass $stdClass)
{
$this->stdClass = $stdClass;
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector\Fixture;

use stdClass;
use Symfony\Contracts\Service\Attribute\Required;

trait WithRequiredAttribute
{
private $service;

public function getService(): stdClass
{
return $this->service;
}

#[Required]
public function setService(stdClass $stdClass)
{
$this->stdClass = $stdClass;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\CodeQuality\Rector\Trait_\AddTraitGetterReturnTypeBasedOnSetterRequiredRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddTraitGetterReturnTypeBasedOnSetterRequiredRector::class);
};
Loading

0 comments on commit 928f117

Please sign in to comment.