Given a function which behaves as either a mutator or accessor depending on the arguments passed to it, like this:
// in PHP, you can pass any number of arguments to a function...
function cache($cacheName) {
$arguments = func_get_args();
if (count($arguments) >= 2) { // two arguments passed. MUTATOR.
$value = $arguments[1]; // use the second as the value
$this->cache[$cacheName] = $value; // *change* the stored value
} else { // 1 argument passed, ACCESSOR
return $this->cache[$cacheName]; // *get* the stored value
}
}
cache('foo', 'bar'); // nothing returned
cache('foo') // 'bar' returned
How do you document this in PHPDoc or a similar automated documentation creator? I had originally just written it like this:
/**
* Blah blah blah, you can use this as both a mutator and an accessor:
* As an accessor:
* #param $cacheName name of the variable to GET
* #return string the value...
*
* As a mutator:
* #param $cacheName name of the variable to SET
* #param $value the value to set
* #return void
*/
However, when this is run through phpDoc, it complains because there are 2 return tags, and the first #param $cacheName description is overwritten by the second.
Is there a way around this?
as you found out, you cannot document 2 different signatures of a single function. what you can do, however - if you use phpDocumentor -, is to document optional function parameters and multiple possible return types:
/**
* Blah blah blah, you can use this as both an accessor and a mutator, e.g.
* <code>cache('name') // get cache value</code>
* and
* <code>cache('name', 'value') // set new cache value</code>.
*
* #param string $cacheName name of the variable to GET|SET
* #param string $value optional new value
*
* #return string|void value of $cacheName or, in case of mutator, void
*/
for clarity, i would also include the usage example.
Related
i would like to define new class with extends of PDOStatement and in this child class i need override function bindColumn (PDOStatement::bindColumn) and in this override function call parent::bindColumn(). But i can't find default value for this method. Is there way to find default value in any method which i would like to override? In source code or somewhere? I need override more function and i would like to find any default value. Thanks
class myStatement extends PDOStatement
{
public function bindColumn ($column, &$param, $type = ?, $maxlen = ?, $driverdata = ?)
{
}
}
The only reason why I'm posting this as answers is that I can't post it as a comment.
/**
* (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)<br/>
* Bind a column to a PHP variable
* #link http://php.net/manual/en/pdostatement.bindcolumn.php
* #param mixed $column <p>
* Number of the column (1-indexed) or name of the column in the result set.
* If using the column name, be aware that the name should match the
* case of the column, as returned by the driver.
* </p>
* #param mixed $param <p>
* Name of the PHP variable to which the column will be bound.
* </p>
* #param int $type [optional] <p>
* Data type of the parameter, specified by the PDO::PARAM_* constants.
* </p>
* #param int $maxlen [optional] <p>
* A hint for pre-allocation.
* </p>
* #param mixed $driverdata [optional] <p>
* Optional parameter(s) for the driver.
* </p>
* #return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
*/
public function bindColumn ($column, &$param, $type = null, $maxlen = null, $driverdata = null) {}
Get yourself a decent IDE man, it works wonders.
The signature of this method is:
public function bindColumn ($column, &$param, $type = null, $maxlen = null, $driverdata = null) {}
You can use modern IDE like PHPStorm for example to find the signature of any method and other useful information
How is it possible to use this function using the PHP API. I didn't find a method for that.
curl -XGET 'localhost:9200/_analyze?analyzer=standard' -d 'this is a test'
I need to access it, because I need to have access to the query string calculating my score via script_score. The query string isn't analysed, of course. So I have to do that separately.
I know that's not a good solution, but I didn't find an alternative until now.
It's in the Indices name space:
/**
* $params['index'] = (string) The name of the index to scope the operation
* ['analyzer'] = (string) The name of the analyzer to use
* ['field'] = (string) Use the analyzer configured for this field (instead of passing the analyzer name)
* ['filters'] = (list) A comma-separated list of filters to use for the analysis
* ['prefer_local'] = (boolean) With `true`, specify that a local shard should be used if available, with `false`, use a random shard (default: true)
* ['text'] = (string) The text on which the analysis should be performed (when request body is not used)
* ['tokenizer'] = (string) The name of the tokenizer to use for the analysis
* ['format'] = (enum) Format of the output
* ['body'] = (enum) Format of the output
*
* #param $params array Associative array of parameters
*
* #return array
*/
public function analyze($params = array())
{
$index = $this->extractArgument($params, 'index');
$body = $this->extractArgument($params, 'body');
/** #var callback $endpointBuilder */
$endpointBuilder = $this->dicEndpoints;
/** #var \Elasticsearch\Endpoints\Indices\Analyze $endpoint */
$endpoint = $endpointBuilder('Indices\Analyze');
$endpoint->setIndex($index)
->setBody($body);
$endpoint->setParams($params);
$response = $endpoint->performRequest();
return $response['data'];
}
https://github.com/elasticsearch/elasticsearch-php/blob/master/src/Elasticsearch/Namespaces/IndicesNamespace.php
I found this question that had been answered, but it didn't solve my problem:
Aptana Scriptdoc doesn't show up in Code Assist
Using the PHP equivalent of their example...
/**
* Gets the current foo
* #param {String} $fooId The unique identifier for the foo.
* #return {Object} Returns the current foo.
*/
public function getFoo($fooId) {
return $bar[$fooId];
}
However, the documenation provided looks like this (extra ending braces included):
getFoo($fooId)
Gets the current foo
#param String $fooId The unique identifier for the foo.
#return Object}
Resolved return types: Object}
Please let me know what I'm doing wrong.
Thanks!
The #return type should not be wrapped with curly brackets.
Your doc should look like this:
/**
* Gets the current foo
* #param String $fooId The unique identifier for the foo.
* #return Object Returns the current foo.
*/
public function getFoo($fooId) {
return $bar[$fooId];
}
The parsing of the return type follows the PHPDoc #return rules.
This also means you can have a mixed return type, which will give you code-assist suggestions from multiple types.
For example:
/**
* #return MyClass|PDO doc doc doc
*/
Cheers
When creating form elements with Zend (using Zend Studio for Eclipse), I'd like some auto completion or hints. Here's what I'm thinking. I'm sure these exist, but I don't know how to get them.
I type createElement and auto-completes gives me the signature createElement($type, $name). Great, I select it.
but when I try to set the $type I don't get any hints like DateTextBox or ValidationTextBox. Being new, I see how this can be useful. What do you do to remember all the options?
for the array of attributes like require, invalidMessage, I'd like to get a list of those to choose from, and/or auto-complete when I start typing one.
// Date field
$date = $this->createElement('DateTextBox', 'date',
array('require' => 'true', 'invalidMessage' => 'Invalid date format')
);
$date->setLabel('date')->setRequired(true);
You have few options to help yourself, without waiting for any plugin:
learn it and remember ;)
extend your phpDoc blocks with all available options:
Example (to be honest I don't know if Eclipse supports html in phpDoc or even any text after variable name in #param, but it works fine in Netbeans):
/**
* [...]
* #param string $type Can be: <ul><li>DateTextBox</li><li>ValidationTextBox</li></ul>
* #param string $name Whatever
* #param array|Zend_Config $options Array with following keys: <ul><li>require</li><li>invalidMessage</li></ul>
* #return Zend_Form_Element
*/
public function createElement($type, $name, $options = null)
extend Zend class and create your own methods to simplify your work
Example:
class My_Zend_Form_Element extends Zend_Form_Element
{
public function createDateTextBox($name, $options = null)
{
return $this->createElement('DateTextBox', $name, $options);
}
}
declare some well named constants and provide some hint in phpDoc
Example: (type ZFE_OPTIONS and IDE should show hint with some constants to use as array keys)
/**
* Can be true or false
*/
define('ZFE_OPTIONS_REQUIRE','require');
create your own helper classes with methods to produce valid options array
Example:
class ZFE_Options
{
protected $opts = array();
/**
* #param bool $req
* #return ZFE_Options
*/
public function setRequired($req){
$this->opts['require'] = (bool)$req;
return $this;
}
/**
* #param string $txt
* #return ZFE_Options
*/
public function setInvalidMessage($txt){
$this->opts['invalidMessage'] = (string)$txt;
return $this;
}
/**
* #return array
*/
public function toArray(){
return $this->opts;
}
}
$zfe_options = new ZFE_Options();
$opts = $zfe_options
->setRequired(true)
->setInvalidMessage('Please provide valid email address')
->toArray();
That's not possible. It's not how autocompletion works. The hints you get are taken directly from ZF's code documentation. Nothing more, nothing less. Everything you see as hints is taken directly from the DocBlock and method signature, e.g.
/**
* Create an element
*
* Acts as a factory for creating elements. Elements created with this
* method will not be attached to the form, but will contain element
* settings as specified in the form object (including plugin loader
* prefix paths, default decorators, etc.).
*
* #param string $type
* #param string $name
* #param array|Zend_Config $options
* #return Zend_Form_Element
*/
public function createElement($type, $name, $options = null)
Eclipse can tell you to insert a string or an array and it will know that the method returns a Zend_Form_Element, but it cannot tell you what these strings should be.
The only place where I know something like what you describe exists is for CSS files. For some reason, when I type in display: it will give me an autocomplete box with possible values for this declaration. If you want more sophisticated autocomplete like this, consider filing this as a feature request to Zend.
How do I doc a variable number of parameters? I am writing an application in PHP and JavaScript. Currently I have (in JavaScript):
/**
* Description
* #param {String} description
*/
function fn()
{
// arguments holds all strings.
}
So, how do I doc n-number of string params?
E.g. PhpDocumentor suggests using an ellipsis
/**
* Example of unlimited parameters.
* Returns a formatted var_dump for debugging purposes
* (since the recurrences of $v are not listed in the actual function signature in the code,
* you may wish to highlight they are "optional" in their description)
* #param string $s string to display
* #param mixed $v variable to display with var_dump()
* #param mixed $v,... unlimited OPTIONAL number of additional variables to display with var_dump()
*/
function fancy_debug($s,$v)
{