Zend\Code\Generator\MethodGenerator::generate PHP Метод

generate() публичный Метод

public generate ( ) : string
Результат string
    public function generate()
    {
        $output = '';
        $indent = $this->getIndentation();
        if (($docBlock = $this->getDocBlock()) !== null) {
            $docBlock->setIndentation($indent);
            $output .= $docBlock->generate();
        }
        $output .= $indent;
        if ($this->isAbstract()) {
            $output .= 'abstract ';
        } else {
            $output .= $this->isFinal() ? 'final ' : '';
        }
        $output .= $this->getVisibility() . ($this->isStatic() ? ' static' : '') . ' function ' . ($this->returnsReference ? '& ' : '') . $this->getName() . '(';
        $parameters = $this->getParameters();
        if (!empty($parameters)) {
            foreach ($parameters as $parameter) {
                $parameterOutput[] = $parameter->generate();
            }
            $output .= implode(', ', $parameterOutput);
        }
        $output .= ')';
        if ($this->returnType) {
            $output .= ' : ' . $this->returnType->generate();
        }
        if ($this->isAbstract()) {
            return $output . ';';
        }
        if ($this->isInterface()) {
            return $output . ';';
        }
        $output .= self::LINE_FEED . $indent . '{' . self::LINE_FEED;
        if ($this->body) {
            $output .= preg_replace('#^((?![a-zA-Z0-9_-]+;).+?)$#m', $indent . $indent . '$1', trim($this->body)) . self::LINE_FEED;
        }
        $output .= $indent . '}' . self::LINE_FEED;
        return $output;
    }

Usage Example

Пример #1
0
    /**
     * @group ZF-7268
     */
    public function testDefaultValueGenerationDoesNotIncludeTrailingSemicolon()
    {
        $method = new MethodGenerator('setOptions');
        $default = new ValueGenerator();
        $default->setValue(array());

        $param   = new ParameterGenerator('options', 'array');
        $param->setDefaultValue($default);

        $method->setParameter($param);
        $generated = $method->generate();
        $this->assertRegexp('/array \$options = array\(\)\)/', $generated, $generated);
    }