Skip to content

[PropertyAccess] Allow custom methods on property accesses #18016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Annotation renamed to @Property
  • Loading branch information
lrlopez committed Jul 3, 2016
commit e4aaac903a7bea8b30b83c509d3ddca76b9a4d9c
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class PropertyAccessor
class Property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annotation duplicates the other annotations. I don't think we should add multiple ways to add the same thing:

  • more code to maintain
  • harder to improve performance
  • harder to introduce changes consistently
  • harder to understand for our users when to use which

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are also some advantages of using property annotations:

  • the annotation is next to the property declaration, which is very convenient when looking for it. On the other hand, method annotations are spread out in the class definition (properties are usually placed on the top of the class and method implementations at the end)
  • you can override several methods with just one annotation

Property annotations are implemented with just 19 lines of code (not taking into account the empty annotation class). In fact, all new annotations are loaded from the same method AnnotationLoader->loadClassMetadata(), so the code will be easy to maintain and optimize as changes would be centralized.

About being harder to understand for newcomers, we could just give them some initial guidelines (i.e. method annotations for virtual properties and property annotations for everything else) and let them use whatever they want, just like the YAML vs XML vs Annotation debate.

Anyway, if you feel that only method annotations must be kept, I'll do that, of course!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @webmozart, it's weird to have different way to do the same thing. What about having only a @Property annotation with an optional name attribute in case you use it on a method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas @webmozart 👍 for having only one annotation. @Property looks weird on a method, maybe @Accessor ?

{
/**
* Custom setter method for the property
* Custom setter method for the property.
*
* @var string $setter
* @var string
*/
public $setter;

/**
* Custom getter method for the property
* Custom getter method for the property.
*
* @var string $setter
* @var string
*/
public $getter;

/**
* Custom adder method for the property
* Custom adder method for the property.
*
* @var string $setter
* @var string
*/
public $adder;

/**
* Custom remover method for the property
* Custom remover method for the property.
*
* @var string $setter
* @var string
*/
public $remover;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\PropertyAccess\Mapping\Loader;

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\PropertyAccess\Annotation\PropertyAccessor;
use Symfony\Component\PropertyAccess\Annotation\Property;
use Symfony\Component\PropertyAccess\Mapping\PropertyMetadata;
use Symfony\Component\PropertyAccess\Mapping\ClassMetadata;

Expand Down Expand Up @@ -56,7 +56,7 @@ public function loadClassMetadata(ClassMetadata $classMetadata)

if ($property->getDeclaringClass()->name === $className) {
foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
if ($annotation instanceof PropertyAccessor) {
if ($annotation instanceof Property) {
$propertiesMetadata[$property->name]->setGetter($annotation->getter);
$propertiesMetadata[$property->name]->setSetter($annotation->setter);
$propertiesMetadata[$property->name]->setAdder($annotation->adder);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/PropertyAccess/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@

namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

use Symfony\Component\PropertyAccess\Annotation\PropertyAccessor;
use Symfony\Component\PropertyAccess\Annotation\Property;

/**
* Fixtures for testing metadata.
*/
class Dummy
{
/**
* @PropertyAccessor(getter="getter1", setter="setter1", adder="adder1", remover="remover1")
* @Property(getter="getter1", setter="setter1", adder="adder1", remover="remover1")
*/
protected $foo;

/**
* @PropertyAccessor(getter="getter2")
* @Property(getter="getter2")
*/
protected $bar;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

use Doctrine\ORM\Mapping\Column;
use Symfony\Component\PropertyAccess\Annotation\PropertyAccessor;
use Symfony\Component\PropertyAccess\Annotation\Property;

class TestClass
{
public $publicProperty;
protected $protectedProperty;
private $privateProperty;

private $publicAccessor;
private $publicMethodAccessor;
Expand All @@ -32,7 +30,7 @@ class TestClass
private $date;

/**
* @PropertyAccessor(getter="customGetterTest", setter="customSetterTest")
* @Property(getter="customGetterTest", setter="customSetterTest")
*/
private $customGetterSetter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PropertyAccessorCollectionTest_Car
private $axes;

/**
* @Symfony\Component\PropertyAccess\Annotation\PropertyAccessor(adder="addAxisTest", remover="removeAxisTest")
* @Symfony\Component\PropertyAccess\Annotation\Property(adder="addAxisTest", remover="removeAxisTest")
*/
private $customAxes;

Expand Down