Nette\PhpGenerator\Method::__toString PHP Method

__toString() public method

public __toString ( ) : string
return string PHP code
    public function __toString()
    {
        $parameters = [];
        foreach ($this->parameters as $param) {
            $variadic = $this->variadic && $param === end($this->parameters);
            $hint = $param->getTypeHint();
            $parameters[] = ($hint ? ($this->namespace ? $this->namespace->unresolveName($hint) : $hint) . ' ' : '') . ($param->isReference() ? '&' : '') . ($variadic ? '...' : '') . '$' . $param->getName() . ($param->isOptional() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
        }
        $uses = [];
        foreach ($this->uses as $param) {
            $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
        }
        return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . ($this->visibility ? $this->visibility . ' ' : '') . ($this->static ? 'static ' : '') . 'function' . ($this->returnReference ? ' &' : '') . ' ' . $this->name . '(' . implode(', ', $parameters) . ')' . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '') . ($this->returnType ? ': ' . ($this->namespace ? $this->namespace->unresolveName($this->returnType) : $this->returnType) : '') . ($this->abstract || $this->body === FALSE ? ';' : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
    }

Usage Example

Example #1
0
 /**
  * @return string
  */
 public function __toString()
 {
     $this->setBody('');
     if (strtolower($this->getName()) === '__construct') {
         $this->addParameter('_kdyby_aopContainer')->setTypeHint('\\Nette\\DI\\Container');
         $this->addBody('$this->_kdyby_aopContainer = $_kdyby_aopContainer;');
     }
     $this->addBody('$__arguments = func_get_args(); $__exception = $__result = NULL;');
     if ($this->before) {
         foreach ($this->before as $before) {
             $this->addBody($before);
         }
     }
     if ($this->afterThrowing || $this->after) {
         $this->addBody('try {');
     }
     if (!$this->around) {
         $parentCall = Code\Helpers::format('$__result = call_user_func_array("parent::?", $__arguments);', $this->getName());
     } else {
         $parentCall = Code\Helpers::format('$__around = new \\Kdyby\\Aop\\JoinPoint\\AroundMethod($this, __FUNCTION__, $__arguments);');
         foreach ($this->around as $around) {
             $parentCall .= "\n" . $around;
         }
         $parentCall .= "\n" . Code\Helpers::format('$__result = $__around->proceed();');
     }
     $this->addBody($this->afterThrowing || $this->after ? Nette\Utils\Strings::indent($parentCall) : $parentCall);
     if ($this->afterThrowing || $this->after) {
         $this->addBody('} catch (\\Exception $__exception) {');
     }
     if ($this->afterThrowing) {
         foreach ($this->afterThrowing as $afterThrowing) {
             $this->addBody(Nette\Utils\Strings::indent($afterThrowing));
         }
     }
     if ($this->afterThrowing || $this->after) {
         $this->addBody('}');
     }
     if ($this->afterReturning) {
         if ($this->afterThrowing || $this->after) {
             $this->addBody('if (empty($__exception)) {');
         }
         foreach ($this->afterReturning as $afterReturning) {
             $this->addBody($this->afterThrowing || $this->after ? Nette\Utils\Strings::indent($afterReturning) : $afterReturning);
         }
         if ($this->afterThrowing || $this->after) {
             $this->addBody('}');
         }
     }
     if ($this->after) {
         foreach ($this->after as $after) {
             $this->addBody($after);
         }
     }
     if ($this->afterThrowing || $this->after) {
         $this->addBody('if ($__exception) { throw $__exception; }');
     }
     $this->addBody('return $__result;');
     return parent::__toString();
 }