PHPDoc to assign #param as a referened #property - php

I'm trying to define a param for a method as being the ID field from another class. Basically this idea...
/**
* #param \Database\Member::id $memberId
* #return MemberEntity
*/
public function getMember( int $memberId ) : MemberEntity
{
...
}
the #see command doesn't seem to be the right solution for this. Is there something I'm missing here? The PHPDoc website is surprisingly minimal...
thanks!

The #param tag receives the values: type, name and description. Apparently the type of your variable is int. If the \Database\Member::id attribute is not a native type of php or a class, it can not be used that way. So it is not possible to do this type of reference you want in traditional PHPDoc.
Syntax
#param [Type] [name] [<description>]

Related

Is there any way of using an object type in #property-read and "declare" its attributes?

I want to add a type of object in #property-read and also be able to access its attributes with PhpStorm suggestions.
Here's what I would like to do:
/**
* #property-read object{type: string, schema: string} $request_schema
* #property-read object{type: string, schema: string} $response_schema
*/
class ConfigApiRoutesSchema extends BaseSchema
{
...
}
The reason I'm doing this is because I want to access the attributes type and schema, like this:
$configApiRoutesSchema = new ConfigApiRoutesSchema();
$configApiRoutesSchema->request_schema->type // Here's the problem, type is not suggested
$configApiRoutesSchema->request_schema->schema // schema is also not suggested
This is exactly what I need, but unfortunately this doesn't work for some reason. One solution is to create two classes and specify them as types:
/**
* #property-read string $type
* #property-read string $schema
*/
class RequestSchema {}
/**
* #property-read string $type
* #property-read string $schema
*/
class ResponseSchema {}
/**
* #property-read RequestSchema $request_schema
* #property-read ResponseSchema $response_schema
* #
*/
class ConfigApiRoutesSchema extends BaseSchema {}
Now PhpStorm suggests both schema and type.
But it doesn't seem correct, since RequestSchema and ResponseSchema won't actually be used. So I wonder if there's any way of using annotation with objects without having to create a class to suggest its attributes.
Thank you.
Altho it is possible to specify the shape of arrays, for objects it is not possible to do so (yet). Such a syntax would have been be specified in PSR-5 and PSR-19, but this is not the case.
In the same manner it is not possible to specify the properties of a returned plain object from a method with the #return object annotion.
Apparently the syntax is supported by Psalm, but not in PhpStorm (at very least not yet).
Currently there is a public proposal on the PhpStorm YouTrack board. You might want to upvote that issue to increase it's chance of being included in the next PhpStorm version.

How to use class constants in #Security annotation using the Symfony Expression Language?

I am using Symfony 3 and I've created a custom Voter class.
I want to access it using the SensioFrameworkExtraBundle #Security tag.
It kind of works.
If I do the following it works perfectly:
/**
* #Rest\Get("organisation/{id}")
* #Security("is_granted('OrgAdmin', id)")
* #param int $id
* #param Request $request
*
* #return View
*/
public function getOrganisationAction($id, Request $request)
{
But I don't like the idea of using magic strings in the application and I would much rather use a class constant for the check.
Something like this:
/**
* #Rest\Get("organisation/{id}")
* #Security("is_granted(AppBundle\OrgRoles::ROLE_ADMIN, id)")
* #param int $id
* #param Request $request
*
* #return View
*/
public function getOrganisationAction($id, Request $request)
{
But when I try that I get the following error message:
Unexpected character \"\\\" around position 20 for expression `is_granted(AppBundle\\OrgRoles::ROLE_ADMIN, id)`.
Which when unescaped, is the following:
Unexpected character "\" around position 20 for expression `is_granted(AppBundle\OrgRoles::ROLE_ADMIN, id)`.
So I'm stumped on this.
Can it be done?
Any suggestions on a better way to do this?
You can use the constant() function available in the Expression Language Component:
#Security("is_granted(constant('\\Full\\Namespace\\To\\OrgRoles::ROLE_ADMIN'), id)")
Doctrine annotation reader has made this even easier for constants in PHP code:
use MyCompany\Annotations\Bar;
use MyCompany\Entity\SomeClass;
/**
* #Foo(PHP_EOL)
* #Bar(Bar::FOO)
*/
This also works just as expected for #Security / #IsGranted.
https://www.doctrine-project.org/projects/doctrine-annotations/en/latest/custom.html#constants

What does the #param keyword do in symfony routes

In annotations for a route what does #param mean?
I can not find any documentation on symfony website regarding this keyword and very confused as to what it's for and does.
I'm asking because I'm trying to figure out how to access parameters in my .yml files for use in the routes but I'd like to still use annotations instead of the YML method of routing.
Showing a code example is probably not the best for this but here is where #param shows up
/**
* League action
*
* #Route("/association/{assoc}/{league}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null})
* #Route("/association/{assoc}/{league}/{game}")
* #Template()
*
* #param $assoc
* #param $league
* #param $game
* #return array
*/
I took that from here (Symfony2 route in annotations with optional parameters)
Any help is appreciated, thanks!
#param and #return are phpDocumentor annotations, and do not actually affect routing.

Retrieving GET and POST data inside Laravel controller

I've been searching the web for how to get POST data inside the controller, so far I have found two solutions: Input::get() and $_POST.
The comment for Input::get() reads:
/**
* Gets a "parameter" value.
*
* This method is mainly useful for libraries that want to provide some flexibility.
*
* Order of precedence: GET, PATH, POST
*
* Avoid using this method in controllers:
*
* * slow
* * prefer to get from a "named" source
*
* It is better to explicitly get request parameters from the appropriate
* public property instead (query, attributes, request).
*
* #param string $key the key
* #param mixed $default the default value
* #param Boolean $deep is parameter deep in multidimensional array
*
* #return mixed
*/
What is this "named" source they refer to? What is it I should use instead of Input::get() ?
The documentation shows you can retrieve an input value for any HTTP verb by using Input::get().
$name = Input::get('name');
TO get all the inputs use Input::all() method.
To check if specific column exists use Input::has('column_name') eg.Input::has('name').
To retrieve column value use Input::get('column_name') eg. Input::get('name').
You can get a parameter from url using :-
request()->urlParam;
if you want to get GET parameter using :-
$request->get('current-password');
if you want to get POST parameter using :-
$request->post('current-password');
In modern Laravel installs, if your controller method is passed an instance of Request then you can use that. For example, all these are identical:
public function update(Request $request, Model $model) {
$some_var = $_POST["some_var"];
$some_var = $request->input("some_var");
$some_var = $request->post("some_var");
$some_var = $request->some_var;
}
If your method is not passed an instance of the current Request you can use the request() helper method to access one.

How to set member type in Doxygen for PHP code?

I'm trying to document PHP class members with Doxygen (I'm not using PHPDocs because the project involves COCOA programming, so I can use the same tool for both parts).
/**
* This is brief description.
*
* This is detailed description.
*/
private $foo;
This code gets the documentation done right, but I would like to include in the docs the type the var should handle.
I tried to use \var and \property, but then Doxigen does not generate the doc for the var.
//THIS IS NOT WORKING!
/**
* This is brief description.
*
* This is detailed description.
* \var int
*/
private $foo;
I have seen this post:
Doxygen: how to describe class member variables in php?
Seems that \var is not working in Doxyegn, but the post is a bit old and maybe there is something I can do now.
I have follow the bug comments of this feature:
https://bugzilla.gnome.org/show_bug.cgi?id=626105.
In comment number 6 a solution is proposed, adding the var name after the type.
class Mine {
/**
* Definition of variable
* #var string $var
*/
private $var = array();
}
This is working for me.
Simple workaround which generates acceptable results is to add this input filter:
INPUT_FILTER = "sed -e 's/#var\s/#see /'"
Or even better to define an alias:
ALIASES += "var=#see"
It simply replaces #var command with #see command. It is not perfect, but it is very simple and relatively bulletproof.
Small disadvantage is that the type is somewhere in the description instead of the heading. On the other side, if only few properties has type defined, it makes documentation more consistent (headings look the same).
#type works for me:
/** #type string[] */
private $csvData;
/**
* command line parameters
* #type string[]
*/
private $parameters;

Categories