Doctrine 2 annotations and "var" - php

Could someone tell me whats the meaning of "#var" in annotations configuration?
For example:
/**
* #Column(type="string", length=20, unique=TRUE)
* #var string
*/
protected $login;

It tells you what type of variable is it. Whether it's integer, string or an object, for example. It's used for auto-documentating processes

This is an annotation used for automatic documentation generation with phpDocumentor. For #var see their documentation.

Related

JMS serializer. How to use Exclude condition?

i'm using JMS serializer in my symfony project and i have a question about "Exlude condition". Is it possible to specify that one property of entity would be exlude for all routes (methods) exept one.
I mean something like this:
/**
* #var string
*
* #ORM\Column(name="full_name", type="text", nullable=true)
* #JMS\Exlude(if="!someAction()")
*/
private $fullName;
If this possible, what is the correct syntax for this? Thanks)
To utilize #Exclude annotation, you need to have a bit of Symfony's ExpressionLanguage understanding.
Obviously, the function used in the annotation (e.g. someAction()) does not belong to the current object but rather to Expression language instance. To register it, do the following:
$language = new ExpressionLanguage();
$language->register('someAction', function(){}, function ($arguments, $object) {
// your logic goes here
return false;
});
Then bind it to your serializer:
$serializer = SerializerBuilder::create()
->setExpressionEvaluator(new ExpressionEvaluator($language))
->build();
Then you should be able to serialize using this exclusion strategy:
/**
* #var string
*
* #ORM\Column(name="full_name", type="text", nullable=true)
* #JMS\Exlude(if="!someAction(object)")
*/
private $fullName;
The one thing I am unsure of is passing empty callable to register call (for compiler) and I have no means of giving it a spin and confirming that is valid.
Hope this helps...

Where in the PSR standards does it say to not have the variable name after the type declaration?

Can anyone advise where in the PSR standards docs it documents that there should be nothing after the variable type in class member variables?
I used PHP CodeStyle Fixer by doing the following: php-cs-fixer fix MyConsoleCommand.php
...and it removed the variable from the docblock. I have been doing this for some time now and I believed this was correct and standards-compliant.
Can anyone confirm?
/**
- * #var SiteManager $siteManager
+ * #var SiteManager
*/
private $siteManager;
/**
- * #var Registry $doctrine
+ * #var Registry
*/
private $doctrine;
By default all the levels are on and this particulair behaviour comes from the sympfony standard
phpdoc_var_without_name [symfony]
#var and #type annotations should not contain the variable name.
see https://github.com/FriendsOfPHP/PHP-CS-Fixer and search for "phpdoc_var_without_name"
if you don't want to use the Symfony standard do
php php-cs-fixer.phar fix MyConsoleCommand.php --level=psr2
hope it helps

Doctrine Is not a valid entity or mapped super class

i have a problem with doctrine and i getting this error from auto generated entity file "Class "Users" is not a valid entity or mapped super class.". File and comments inside looks like fine i dont understund why or i something miss?
Some piece of code
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* #ORM\Table(name="users", uniqueConstraints={#ORM\UniqueConstraint(name="username", columns={"username"})})
* #ORM\Entity
*/
class Users
{
/**
* #var integer
*
* #ORM\Column(name="userid", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userid;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=100, nullable=false)
*/
private $username;
Doctrine 2 annotation mapping might have been configured to negate the need for the #ORM prefix.
I would try replacing #ORM\ with #. For example #Entity
As far as i recall, these errors happen when doctrine cant find the entity, double check the namespace, by default the entity folder in symfony is "Entity" (Uppercase!). Also check the config files if auto_mapping is set to true.
for me this problem was solved after adding following namespace
use Doctrine\Common\Annotations\AnnotationReader;
in my doctrine.php

PHP #var Documentation pattern

I did read a lot about PHP documentation and how to do the comments inside the classes, but I do not know yet if have any pattern to identify the size of a variable. I didn't saw any convention that talk about this kind of information. What I'm doing a Web Service and some parameters has a required size. Example:
What I got:
/**
*
* #access private
* #var string
*/
private $password;
What I want:
/**
*
* #access private
* #var string 10 (<-- size)
*/
private $password;
What I want to know if I write the size after identifying the type is correct. Or if have any way to do that or if I don't have to do.
Theres no need to had the size, actually there's nothing regarding the size in phpdoc
it should be only
/**
* #var <type> [$<var name>]
*/
I hope this helps :)

Is there any reason not to group class properties?

I use docblocks to typehint class properties, and only recently realised that class properties may be grouped as such:
/**
* #var string
*/
private $firstname, $surname, $location;
Which is way easier to read, and shorter, than this:
/**
* #var string
*/
private $firstname;
/**
* #var string
*/
private $surname;
/**
* #var string
*/
private $location;
Would there be any reason not to do this?
I prefer 1st approach, because PSR-2 says "There MUST NOT be more than one property declared per statement.". There are good reasons for that, and here are some of my thoughts I guess authors of PSR-2 had too:
people don't expect to see more than one declaration per line (at least me and my collegues);
it's more difficult to change type of a variable, e.g. of $location from string to a Location class.
it's more difficult to read diffs: the line will be marked as changed by VCS if name of any variable changes, and reviewer will have to read the whole line. Such line has higher probability of changing;
it's not easy to comment a single variable from a group.

Categories