I am using PHP ReflectionClass to extract as much information about a class as possible. Can I also use this to get return values from functions? Or does that not make sense since reflection only profiles what an object accepts?
You can not rely, if a function has a well defined return value you can simply extract from the source code. Imagine something like this:
return $this->isValid() ? $result : $this->createNullObject();
Thats hard (/impossible) to parse just to get the return value. You can use DocComments instead. #return is the usual tag for that use
/**
* MyMethod
*
* #return int
*/
Call getDocComment() on a ReflectionMethod-object and then parse the docComment.
for internal functions you could use
$reflect = new ReflectionExtension('standard');
echo "<pre>" . $reflect . "</pre>";
Related
With codeception is there a way to use constants ( or variables) using the #example annotation ?
I know that we can use doctrine style annotation.
Data is defined via the #example annotation, using JSON or Doctrine-style notation (limited to a single line). Doctrine-style
class PageCest
{
/**
* #example(url="/", title="Welcome")
* #example(url="/info", title="Info")
* #example(url="/about", title="About Us")
* #example(url="/contact", title="Contact Us")
*/
public function staticPages(AcceptanceTester $I, \Codeception\Example $example)
{
$I->amOnPage($example['url']);
$I->see($example['title'], 'h1');
$I->seeInTitle($example['title']);
}
}
But using a constant which is working in doctrine doesn't seems to work here
/**
* #example(url=Class::Constant, title="Welcome")
*/
is there a way to achieve this to run multiple test examples but use constants or variables to provide the values?
It seems that there is no way to do that directly like doctrine's implementation.
the workaround I found is to pass the constant name as a string .
/**
* #example(url="MyConstantName", title="Welcome")
*/
and then within the code use the constant function to find your constant value.
constant('Full\Namespace\MyClass::' . $example['MyConstantName'])
What can I use other than if(!empty( $product->a_funky_function() )) to check if the method is empty before calling it?
I've tried method_exists() and function_exists() and a whole plethora of conditions. I think the issue is that I need to have my $product variable there.
Please help.
A fairly common pattern is to have two methods on your class, along the lines of getField and hasField. The former returns the value, and the latter returns true or false depending whether or not the value is set (where "set" can mean not null, or not empty, or whatever else you might want it to mean).
An example:
class Foo
{
/** #var string */
private $field;
/**
* #return string
*/
public function getField()
{
return $this->field;
}
/**
* #return bool
*/
public function hasField()
{
return $this->getField() !== null;
}
}
This would then be used like:
if ($foo->hasField()) {
$field = $foo->getField();
...
}
Often, like in this example, the has... method often just delegates to the get... method internally, to save duplicating the logic. If the getter contains particularly heavy processing (e.g. a database lookup or API call), you might want to factor in ways to avoid performing it twice, but that's a bit out of scope.
You need absolutely to call it so it can compute the value which it will return, so I think there is no other way to know if it will return something not empty without calling it...
You have to call it.
$result = $product->getFunction();
if(!empty($result)) {
//your code here
}
How i can create a PHPDoc block for an declarative array?.
For example, let's say that i have the next function:
/**
* #return ??????????
*/
function dummy() {
$x1=array("key1"=>"hello","ke2"=>"world");
return $x1;
}
// ... later
$x1=dummy();
echo $x1["key1"];
I want to be explicit in the array result, instead of use #return array.
(i also tried) I also know that i can return an array of object with #return Class[] but in this case, im not using classes.
Thanks.
I want to fetch a method's comments,take below method for example:
/**
* Returns the regex to extract all inputs from a file.
* #param string The class name to search for.
* #return string The regex.
*/
public function method($param)
{
//...
}
the result should be
Returns the regex to extract all inputs from a file.
#param string The class name to search for.
#return string The regex.
the way I find is use a function like file_get_content to get file content -> filter the method I want -> fetch the comment use regexp
it seems a bit complicated , is there any convenient way to archive this?
actually you can get a method's doc comments with getDocComment
$ref=new ReflectionMethod('className', 'methodName');
echo $ref->getDocComment();
If you want to use the comment in PHP for something check out getDocComment in php's reflection api
PHP Doc. Like Java Doc.
For a method dump I use this little function I composed.
It fetches all methods from provided class that are public(and thus of use to you).
I personally use a dump() method to nicely format the outputted array of method names and descriptions, but that's not needed if you wish to use it for something else :-)
function getDocumentation($inspectclass) {
/** Get a list of all methods */
$methods = get_class_methods($inspectclass);
/** Get the class name */
$class =get_class($inspectclass);
$arr = [];
foreach($methods as $method) {
$ref=new ReflectionMethod( $class, $method);
/** No use getting private methods */
if($ref->isPublic()) {
$arr[$method] = $ref->getDocComment();
}
}
/** dump is a formatting function I use, feel free to use your own */
return dump($arr);
}
echo getDocumentation($this);
When I define an object of a class using new like this
$blah = new Whatever();
I get autocomplete for $blah. But how do I do it when I have $blah as a function parameter? Without autocomplete I am incomplete.
Edit: How do I do it if it's in an include and PDT or Netbeans can't figure it out? Is there any way to declare types for variables in PHP?
Method in first comment is called "type hinting", but you should use that wisely. Better solution is phpDoc.
/**
* Some description of function behaviour.
*
* #param Whatever $blah
*/
public function myFunction($blah)
{
$blah->
// Now $blah is Whatever object, autocompletion will work.
}
You can also use an inline phpDoc comment which does exactly the same thing.
public function myFunction($blah)
{
/* #var $blah Whatever */
$blah->
// Now $blah is Whatever object, autocompletion will work.
}
Try to pass parameter class definition into the function:
function myFunction(Whatever $blah) {
}