PHPdoc does not detect object (and other properties) - php

I am trying to create a PHPdocument for my project with the correct information.
I am trying to create information for objects, created inside a method of a class.
Sadly, PHPdoc does not recognize my object inside my function.
The code is as following:
class app_controll
{
/**
* This function starts the application. All the functionality starts here.
* #return Objects Method returns all the objects and functions needed to build a page.
*/
public function start_application()
{
/**
* The domain_controll object contains domain information.
* #var object domain_controll
*/
$oDomain_controll = new domain_controll();
}
}
What am I defining wrong?

class app_controll
{
/**
* This function starts the application. All the functionality starts here.
* #return Objects Method returns all the objects and functions needed to build a page.
*/
public function start_application()
{
/**
* The domain_controll object contains domain information.
* #var $oDomain_controll domain_controll
*/
$oDomain_controll = new domain_controll();
}
}
Usage is : #var objectName className

I also asked the question on Github. I got a response from James Pittman:
In this case, $oDomain_controll would be an internal variable, visible
only within the start_application() function. There would be no reason
to include it in API documentation, because it's not usable or
viewable by a consumer of the app_controll class. If you want to make
it a public member of the app_controll class, you should declare it
outside of the start_application() function:
class app_controll()
{
/**
* The domain_controll object contains domain information.
* #var domain_controll
*/
public $oDomain_controll;
public function start_application()
{
$oDomain_controll = new domain_controll();
}
}
Thank you for your answer James :)

Related

phpstan:: Method (methodname) return type with generic interface (interfacename) does not specify its types

I have defined an interface for an ordered list. The class docblock looks like this:
/**
* Interface ListOrderedInterface
* #template ListOrderedElement
*/
Within the method docblocks for this interface, ListOrderedElement is used to ensure that the type of thing being added to the list is consistent. PHPStan runs clean on ListOrderedInterface.php. So far so good.
Next I defined an interface for a factory that makes an ordered list. The definition looks like this:
/**
* Class ListFactoryInterface
*/
interface ListOrderedFactoryInterface
{
/**
* makeList
* #return ListOrderedInterface
*/
public function makeList(): ListOrderedInterface;
}
phpstan is complaining with the warning message "phpstan:: Method makeList return type with generic interface ListOrderedInterface does not specify its types". I don't know how to specify the types for the interface.
Thanks for helping with this.
You need provide a specialization for your ListOrderedInterface in the makeList phpdoc in the #return part.
interface ListOrderedFactoryInterface
{
/**
* This is an example with a concrete type, syntax is <type>
* #return ListOrderedInterface<string>
*/
public function makeList(): ListOrderedInterface;
}
If you need this to generic as well, you would need to add the #template on the factory also and return a generic type from makeList.
/**
* #template T
*/
interface ListOrderedFactoryInterface
{
/**
* #return ListOrderedInterface<T>
*/
public function makeList(): ListOrderedInterface;
}
And in the classes that implement FactoryInterface, add the #implements phpdoc.
/**
* Instead of string put the actual type it should return
* #implements ListOrderedFactoryInterface<string>
*/
class ConcreteFactory implements ListOrderedFactoryInterface
{
}
You can find more examples in the official docs: https://phpstan.org/blog/generics-by-examples

Laravel class Auth

Hi can I ask about this in laravel framework
namespace Illuminate\Support\Facades;
/**
* #see \Illuminate\Auth\AuthManager
* #see \Illuminate\Contracts\Auth\Factory
* #see \Illuminate\Contracts\Auth\Guard
* #see \Illuminate\Contracts\Auth\StatefulGuard
*/
class Auth extends Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor()
{
return 'auth';
}
}
what does the return 'auth' exactly returning to the caller ? is it text 'auth' or an object ? and what is the reason why they only have one method in that class ? I apologize i am just learning oop.
Thank you in advance.
In this case as you see method getFacadeAccessor it's returning auth string.
Facades are just "shortcuts" to use other classes but in fact you shouldn't use them everywhere if you don't need to.
In Laravel you can bind objects/classes into Application. So you can write for example:
$app->bind('something', function() {
return new SomeObject();
});
Let's assume there is method doSomething in SomeObject class.
Now you can use this method using:
$app['something']->doSomething();
But you can also create facade:
class GreatClass extends Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor()
{
return 'something';
}
}
and now anywhere in your application you could use:
GreatClass::doSomething();
Answering your question this getFacadeAccessor is returning only the name the name of object that is used when bound to Application. To know how it's used you can look into the source of:
/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php
The method you should look first is getFacadeRoot - because this method is returning the requested object.

PHP docstring #return type for subclass of current class

What is the canonical #return type for a static method of a parent class that returns an instance of a subclass, undetermined at time of writing? For example:
<?php
class Thing {
/**
* Do something
*
* #return ??? Instance of some new class.
*/
public static function create_subclass($class) {
return new $class();
}
}
class Person extends Thing {}
$person = Thing::create_subclass('Person');
?>
It seems like this shouldn't be #return self because it's returning a subclass of self, and specifying all possible return classes would be silly. Any ideas?
You can use #return Thing or #return Person|AnotherClass|AnotherClass

TYPO3 6.2 Extbase: Persisted Object still Modified

this may be a Simple issue and im just overseeing it.
What I wanna do:
I have a Model and I simply create an instance of it, fill it with Data and then use the add($object) function of my Repository.
Then I Call the persistenceManager to persist my Entry.
The whole Controller is part of an Backend Module.
I have to Persist the Entry Manualy, because there is a #header Redirect, because of an SSO API Call which redirects me afterwards back.
What is the Problem:
My Debugs hint that after adding Values to the Object the Propertys filled to the Object correctly.
After calling the persistAll function, my Object is Persisted, but the Values appear as "modified".
The result is:
I have an Empty Object in my Table.
Time for some Code (shortened)
/**
* myObjectRepository
*
* #var \MyVendor\MyExt\Domain\Repository\MyObjectRepository
* #inject
*/
protected $myObjectRepository;
/**
* #var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
* #inject
*/
protected $objectManager;
/**
* #var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
* #inject
*/
protected $persistenceManager;
/**
* action verify
*
* #return void
*/
public function verifyAction() {
$myObject = $this->objectManager->get('\\MyVendor\\MyExt\\Domain\\Model\\MyObject');
$myObject->setName('Nice Name');
$myObject->setAge(20);
$this->myObjectRepository->add($myObject);
$this->persistenceManager->persistAll();
}
This is to give an Idea of what I try to do.
I'm kinda stunned with this issue. The normal Behaviour should be a Persisted Database Entry. Maybe its just a Simple detail overseen.
same problem here! Debugging shows, that the persistObject-function called the $dataMap->isPersistableProperty($propertyName) function before pass the data to DB.
this function needs an TCA
/**
* Returns TRUE if the property is persistable (configured in $TCA)
*
* #param string $propertyName The property name
* #return boolean TRUE if the property is persistable (configured in $TCA)
*/
public function isPersistableProperty($propertyName) {
return isset($this->columnMaps[$propertyName]);
}
so, be sure your TCA isset and correct to pass the properties to the DB

Netbeans Auto-Complete Not Working For Custom PHP Class

I've got the following class in a Zend Framework project:
<?php
/**
* User's class
*
* This class should be responsible for all
*
* #author Steve Davies
* #copyright 2012
* #version SVN: $Id$
*/
class Api_Admin_Users extends Api_Core
{
/**
* Class Constructor
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Get User's name
*
* This returns the user's name
*
* #return void
*/
public function new() {
$user = self::_instance()->_em->getRepository('UserManagement\Users')->find('1');
echo $user->getFullName();
}
}
However when I try and use code hinting on $user->getFullName();, it doesn't work.
Using the following trick from here, it works:
/**
* Get User's name
*
* This returns the user's name
*
* #return void
*/
public function new() {
/* #var $user \UserManagement\Users */
$user = self::_instance()->_em->getRepository('UserManagement\Users')->find('1');
echo $user->getFullName();
}
But, I don't want to have to include that comment line everytime I instantiate the object. When I try to move this to the Class definition - or even the method definition, it fails to work.
Can anyone provide an answer for this?
PHP is a dynamic language and as such it is not trivial to infer variable types from static code analysis (like it is in Java for example).
It's especially difficult with factory methods like yours getRepository('UserManagement\Users').
NetBeans currently has no way of knowing how to translate the function argument to the type of returned variable (unless you're satisfied with some parent class from which all subclasses returned by that factory derive). Unfortunatelly vdoc's are the only way to deal with such cases.
Create a method in Api_Admin_Users to access the repository and add the type hint there. This will benefit all methods in the class. As long as the methods in the repository are type-hinted correctly, you're all set.
class Api_Admin_Users extends Api_Core
{
/**
* Class Constructor
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Get the repository
*
* #return \UserManagement\Users
*/
public static function getRepository() {
return self::_instance()->_em->getRepository('UserManagement\Users');
}
/**
* Get User's name
*
* This returns the user's name
*
* #return void
*/
public function new() {
$user = self::getRepository()->find('1');
echo $user->getFullName();
}
}

Categories