Skip to content

Commit

Permalink
Assert JoinColumns in CPP should be converted (#2991)
Browse files Browse the repository at this point in the history
Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
  • Loading branch information
TomasVotruba and greg0ire committed Oct 15, 2022
1 parent f4f758c commit ee222b2
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericA
final class PromotedProperty
{
public function __construct(
#[GenericAnnotation] private string $property,
#[GenericAnnotation]
private string $property,
) {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use JetBrains\PhpStorm\Immutable;

final class MixNonAttributeAndAttributeProperty
{
public function __construct(#[Immutable] public float $latitude, public float $longitude)
public function __construct(#[Immutable]
public float $latitude, public float $longitude)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use JetBrains\PhpStorm\Immutable;

final class PropertyWithAttribute
{
public function __construct(#[Immutable] public float $latitude)
public function __construct(#[Immutable]
public float $latitude)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ use JetBrains\PhpStorm\Immutable;

final class PropertyWithMultipleAttributes
{
public function __construct(#[Immutable] #[Deprecated] public float $latitude)
public function __construct(#[Immutable]
#[Deprecated]
public float $latitude)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\Php80\Rector\Property\NestedAnnotationToAttributeRector\Fixture;

use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\JoinColumns;

final class DoctrineNestedJoinColumnsPromotedProperty
{
public function __construct(
/**
* @JoinColumns({
* @JoinColumn(name="entity_id", referencedColumnName="id"),
* @JoinColumn(name="entity_type", referencedColumnName="entity_type"),
* })
*/
protected $page,
) {
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Property\NestedAnnotationToAttributeRector\Fixture;

use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\JoinColumns;

final class DoctrineNestedJoinColumnsPromotedProperty
{
public function __construct(
#[JoinColumn(name: 'entity_id', referencedColumnName: 'id')]
#[JoinColumn(name: 'entity_type', referencedColumnName: 'entity_type')]
protected $page,
) {
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
Expand Down Expand Up @@ -100,11 +101,11 @@ class SomeEntity
*/
public function getNodeTypes(): array
{
return [Property::class, Class_::class];
return [Property::class, Class_::class, Param::class];
}

/**
* @param Property|Class_ $node
* @param Property|Class_|Param $node
*/
public function refactor(Node $node): ?Node
{
Expand Down
15 changes: 15 additions & 0 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\Node\Scalar\LNumber;
Expand Down Expand Up @@ -483,6 +484,20 @@ protected function pScalar_LNumber(LNumber $lNumber): string|int
return parent::pScalar_LNumber($lNumber);
}

/**
* Keep attributes on newlines
*/
protected function pParam(Param $param): string
{
return $this->pAttrGroups($param->attrGroups)
. $this->pModifiers($param->flags)
. ($param->type instanceof Node ? $this->p($param->type) . ' ' : '')
. ($param->byRef ? '&' : '')
. ($param->variadic ? '...' : '')
. $this->p($param->var)
. ($param->default instanceof Expr ? ' = ' . $this->p($param->default) : '');
}

private function shouldPrintNewRawValue(LNumber|DNumber $lNumber): bool
{
return $lNumber->getAttribute(AttributeKey::REPRINT_RAW_VALUE) === true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use Doctrine\ORM\Mapping as ORM;
class ArrayInAttribtue
{
public function __construct(
#[ORM\ManyToOne(targetEntity: OfferPrice::class, inversedBy: 'myEntities', cascade: ['persist'])] private OfferPrice $offerPrice
#[ORM\ManyToOne(targetEntity: OfferPrice::class, inversedBy: 'myEntities', cascade: ['persist'])]
private OfferPrice $offerPrice
)
{
}
Expand Down

0 comments on commit ee222b2

Please sign in to comment.