Example #1
0
 public function writeMethod(\ReflectionMethod $method)
 {
     $args = [];
     foreach ($method->getParameters() as $parameter) {
         $arg = '';
         if ($parameter->isArray()) {
             $arg .= 'array ';
         } else {
             if ($parameter->getClass()) {
                 $arg .= '\\' . $parameter->getClass()->getName() . ' ';
             }
         }
         if ($parameter->isPassedByReference()) {
             $arg .= '&';
         }
         $arg .= '$' . $parameter->getName();
         try {
             $defaultValue = $parameter->getDefaultValue();
             $arg .= ' = ' . var_export($defaultValue, true);
         } catch (ReflectionException $e) {
             if ($parameter->isOptional()) {
                 $arg .= ' = null';
             }
         }
         $args[] = $arg;
     }
     $modifiers = array_diff(\Reflection::getModifierNames($method->getModifiers()), ['abstract']);
     $methodName = ($method->returnsReference() ? '&' : '') . $method->getName();
     $this->code[] = '  ' . implode(' ', $modifiers) . ' function ' . $methodName . '(' . implode(', ', $args) . ') {';
     $this->code[] = '    $result = $this->' . $this->propertyName . '->invoke($this, \'' . $method->getName() . '\', func_get_args());';
     $this->code[] = '    return $result;';
     $this->code[] = '  }';
 }
Example #2
0
 /**
  * Returns whether this method returns a reference
  *
  * @return boolean True if this method returns a reference
  */
 public function returnsReference()
 {
     if ($this->reflectionSource instanceof ReflectionMethod) {
         return $this->reflectionSource->returnsReference();
     } else {
         return parent::returnsReference();
     }
 }
Example #3
0
 public static function fromReflection(\ReflectionMethod $ref)
 {
     $method = new static();
     $method->setFinal($ref->isFinal())->setAbstract($ref->isAbstract())->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE))->setReferenceReturned($ref->returnsReference())->setName($ref->name);
     if ($docComment = $ref->getDocComment()) {
         $method->setDocblock(ReflectionUtils::getUnindentedDocComment($docComment));
     }
     foreach ($ref->getParameters() as $param) {
         $method->addParameter(static::createParameter($param));
     }
     // FIXME: Extract body?
     return $method;
 }
 public static function fromReflection(\ReflectionMethod $ref)
 {
     $method = new static($ref->name);
     $method->setFinal($ref->isFinal())->setAbstract($ref->isAbstract())->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE))->setReferenceReturned($ref->returnsReference())->setBody(ReflectionUtils::getFunctionBody($ref));
     $docblock = new Docblock($ref);
     $method->setDocblock($docblock);
     $method->setDescription($docblock->getShortDescription());
     $method->setLongDescription($docblock->getLongDescription());
     foreach ($ref->getParameters() as $param) {
         $method->addParameter(static::createParameter($param));
     }
     return $method;
 }
 /**
  * @param \ReflectionMethod $reflectionMethod
  * @param \Donquixote\HastyReflectionCommon\Reflection\FunctionLike\MethodReflectionInterface $methodReflection
  */
 private function assertEqualMethods(\ReflectionMethod $reflectionMethod, MethodReflectionInterface $methodReflection)
 {
     $this->assertEquals($reflectionMethod->isAbstract(), $methodReflection->isAbstract());
     $this->assertEquals($reflectionMethod->getDeclaringClass()->getName(), $methodReflection->getDeclaringClassName());
     $this->assertEquals($reflectionMethod->getDocComment(), $methodReflection->getDocComment());
     $this->assertEquals($reflectionMethod->getShortName(), $methodReflection->getName());
     $this->assertEquals($reflectionMethod->getName(), $methodReflection->getName());
     $this->assertEquals($reflectionMethod->class . '::' . $reflectionMethod->getName(), $methodReflection->getQualifiedName());
     $this->assertEquals($reflectionMethod->returnsReference(), $methodReflection->isByReference());
     $this->assertEquals($reflectionMethod->isPrivate(), $methodReflection->isPrivate());
     $this->assertEquals($reflectionMethod->isProtected(), $methodReflection->isProtected());
     $this->assertEquals($reflectionMethod->isPublic(), $methodReflection->isPublic());
     $this->assertEquals($reflectionMethod->isStatic(), $methodReflection->isStatic());
 }
 public final function generate(\ReflectionMethod $method)
 {
     $name = $method->getName();
     $output = ' public function ';
     if ($method->returnsReference()) {
         $output .= '&';
     }
     $output .= $method->getName() . '(';
     $separator = '';
     foreach ($method->getParameters() as $param) {
         $output .= $separator . $this->generateParameterDeclaration($param);
         $separator = ', ';
     }
     $output .= ") { " . $this->generateBody($name) . " }\n";
     return $output;
 }
 protected function canRedirected(\ReflectionMethod $source, \ReflectionMethod $target)
 {
     if ($target->isPublic() && !preg_match('#^__#', $source->getName()) && $source->returnsReference() == $target->returnsReference()) {
         $sourceParam = $source->getParameters();
         $targetParam = $target->getParameters();
         // check parameters
         if (count($targetParam) != count($sourceParam)) {
             return false;
         }
         foreach ($targetParam as $idx => $to) {
             $from = $sourceParam[$idx];
             if ($to->isArray() != $from->isArray() || $to->getClass() != $from->getClass() || $to->isCallable() != $from->isCallable() || $to->isPassedByReference() != $from->isPassedByReference()) {
                 return false;
             }
         }
         // all ok returns true
         return true;
     }
     return false;
 }
Example #8
0
function methodData(ReflectionMethod $method)
{
    $details = "";
    $name = $method->getName();
    if ($method->isUserDefined()) {
        $details .= "{$name} is user defined\n";
    }
    if ($method->isInternal()) {
        $details .= "{$name} is built-in\n";
    }
    if ($method->isAbstract()) {
        $details .= "{$name} is abstract\n";
    }
    if ($method->isPublic()) {
        $details .= "{$name} is public\n";
    }
    if ($method->isProtected()) {
        $details .= "{$name} is protected\n";
    }
    if ($method->isPrivate()) {
        $details .= "{$name} is private\n";
    }
    if ($method->isStatic()) {
        $details .= "{$name} is static\n";
    }
    if ($method->isFinal()) {
        $details .= "{$name} is final\n";
    }
    if ($method->isConstructor()) {
        $details .= "{$name} is the constructor\n";
    }
    if ($method->returnsReference()) {
        $details .= "{$name} returns a reference (as opposed to a value)\n";
    }
    return $details;
}
Example #9
0
 /**
  * Creates a method code from Reflection
  *
  * @param Method|ParsedMethod $method Reflection for method
  * @param string $body Body of method
  *
  * @return string
  */
 protected function getOverriddenMethod($method, $body)
 {
     $code = preg_replace('/ {4}|\\t/', '', $method->getDocComment()) . "\n" . join(' ', Reflection::getModifierNames($method->getModifiers())) . ' function ' . ($method->returnsReference() ? '&' : '') . $method->name . '(' . join(', ', $this->getParameters($method->getParameters())) . ")\n" . "{\n" . $this->indent($body) . "\n" . "}\n";
     return $code;
 }
 private function GenerateOverridingMethodTemplate(\ReflectionMethod $EntityMethod)
 {
     $MethodTemplate = self::OverriddenMethodTemplate;
     $Modifiers = \Reflection::getModifierNames($EntityMethod->getModifiers());
     $Modifiers[] = 'function';
     if ($EntityMethod->returnsReference()) {
         $Modifiers[] = '&';
     }
     $Modifiers = implode(' ', $Modifiers);
     $Name = $EntityMethod->getName();
     $Parameters = [];
     $ParameterVariables = [];
     foreach ($EntityMethod->getParameters() as $Parameter) {
         $ParameterVariables[] = '$' . $Parameter->getName();
         $Parameters[] = $this->GenerateMethodParameter($Parameter);
     }
     $Parameters = implode(', ', $Parameters);
     $ParameterVariables = implode(', ', $ParameterVariables);
     $MethodTemplate = str_replace('<Modifiers>', $Modifiers, $MethodTemplate);
     $MethodTemplate = str_replace('<Name>', $Name, $MethodTemplate);
     $MethodTemplate = str_replace('<Parameters>', $Parameters, $MethodTemplate);
     $MethodTemplate = str_replace('<ParameterVariables>', $ParameterVariables, $MethodTemplate);
     return $MethodTemplate;
 }
    /**
     * Generates the string representation of a single method: signature, body.
     *
     * @param \ReflectionMethod $reflection_method
     *   A reflection method for the method.
     *
     * @return string
     */
    protected function buildMethod(\ReflectionMethod $reflection_method)
    {
        $parameters = [];
        foreach ($reflection_method->getParameters() as $parameter) {
            $parameters[] = $this->buildParameter($parameter);
        }
        $function_name = $reflection_method->getName();
        $reference = '';
        if ($reflection_method->returnsReference()) {
            $reference = '&';
        }
        $signature_line = <<<'EOS'
/**
 * {@inheritdoc}
 */

EOS;
        if ($reflection_method->isStatic()) {
            $signature_line .= 'public static function ' . $reference . $function_name . '(';
        } else {
            $signature_line .= 'public function ' . $reference . $function_name . '(';
        }
        $signature_line .= implode(', ', $parameters);
        $signature_line .= ')';
        $output = $signature_line . "\n{\n";
        $output .= $this->buildMethodBody($reflection_method);
        $output .= "\n" . '}';
        return $output;
    }
Example #12
0
 protected function buildMethodSignature(\ReflectionMethod $method, bool $skipAbstract = false, bool $skipDefaultValues = false) : string
 {
     if ($method->isProtected()) {
         $code = 'protected ';
     } elseif ($method->isPrivate()) {
         $code = 'private ';
     } else {
         $code = 'public ';
     }
     if ($method->isAbstract()) {
         if (!$skipAbstract) {
             $code .= 'abstract ';
         }
     } elseif ($method->isFinal()) {
         $code .= 'final ';
     }
     if ($method->isStatic()) {
         $code .= 'static ';
     }
     $code .= 'function ';
     if ($method->returnsReference()) {
         $code .= '& ';
     }
     $code .= $method->getName() . '(';
     foreach ($method->getParameters() as $i => $param) {
         if ($i > 0) {
             $code .= ', ';
         }
         $code .= $this->buildParameterSignature($param, $skipDefaultValues);
     }
     $code .= ')';
     if ($method->hasReturnType()) {
         $type = $method->getReturnType();
         if ($type->isBuiltin) {
             $code .= ': ' . $type;
         } else {
             $code .= ': \\' . $type;
         }
     }
     return $code;
 }
 /**
  * Test that the proxy behaves in regard to methods like &foo() correctly
  */
 public function testProxyRespectsMethodsWhichReturnValuesByReference()
 {
     $proxy = $this->_proxyFactory->getProxy('Doctrine\\Tests\\Models\\Forum\\ForumEntry', null);
     $method = new \ReflectionMethod(get_class($proxy), 'getTopicByReference');
     $this->assertTrue($method->returnsReference());
 }
 /**
  * __get() implementation.
  * @param  object
  * @param  string  property name
  * @return mixed   property value
  * @throws MemberAccessException if the property is not defined.
  */
 public static function &get($_this, $name)
 {
     $class = get_class($_this);
     $uname = ucfirst($name);
     $methods =& self::getMethods($class);
     if ($name === '') {
         throw new MemberAccessException("Cannot read a class '{$class}' property without name.");
     } elseif (isset($methods[$m = 'get' . $uname]) || isset($methods[$m = 'is' . $uname])) {
         // property getter
         if ($methods[$m] === 0) {
             $rm = new \ReflectionMethod($class, $m);
             $methods[$m] = $rm->returnsReference();
         }
         if ($methods[$m] === TRUE) {
             return $_this->{$m}();
         } else {
             $val = $_this->{$m}();
             return $val;
         }
     } elseif (isset($methods[$name])) {
         // public method as closure getter
         if (PHP_VERSION_ID >= 50400) {
             $rm = new \ReflectionMethod($class, $name);
             $val = $rm->getClosure($_this);
         } else {
             $val = Nette\Utils\Callback::closure($_this, $name);
         }
         return $val;
     } else {
         // strict class
         $type = isset($methods['set' . $uname]) ? 'a write-only' : 'an undeclared';
         throw new MemberAccessException("Cannot read {$type} property {$class}::\${$name}.");
     }
 }
Example #15
0
 /**
  * @param string           $templateDir
  * @param ReflectionMethod $method
  * @param bool             $cloneArguments
  * @param bool             $callOriginalMethods
  *
  * @return string
  */
 private function generateMockedMethodDefinitionFromExisting($templateDir, ReflectionMethod $method, $cloneArguments, $callOriginalMethods)
 {
     if ($method->isPrivate()) {
         $modifier = 'private';
     } elseif ($method->isProtected()) {
         $modifier = 'protected';
     } else {
         $modifier = 'public';
     }
     if ($method->isStatic()) {
         $modifier .= ' static';
     }
     if ($method->returnsReference()) {
         $reference = '&';
     } else {
         $reference = '';
     }
     if ($this->hasReturnType($method)) {
         $returnType = (string) $method->getReturnType();
     } else {
         $returnType = '';
     }
     if (preg_match('#\\*[ \\t]*+@deprecated[ \\t]*+(.*?)\\r?+\\n[ \\t]*+\\*(?:[ \\t]*+@|/$)#s', $method->getDocComment(), $deprecation)) {
         $deprecation = trim(preg_replace('#[ \\t]*\\r?\\n[ \\t]*+\\*[ \\t]*+#', ' ', $deprecation[1]));
     } else {
         $deprecation = false;
     }
     return $this->generateMockedMethodDefinition($templateDir, $method->getDeclaringClass()->getName(), $method->getName(), $cloneArguments, $modifier, $this->getMethodParameters($method), $this->getMethodParameters($method, true), $returnType, $reference, $callOriginalMethods, $method->isStatic(), $deprecation);
 }
 /**
  * @return bool
  */
 function isByReference()
 {
     return $this->reflectionMethod->returnsReference();
 }
Example #17
0
 /**
  * __get() implementation.
  * @param  object
  * @param  string  property name
  * @return mixed   property value
  * @throws MemberAccessException if the property is not defined.
  */
 public static function &get($_this, $name)
 {
     $class = get_class($_this);
     $uname = ucfirst($name);
     $methods =& self::getMethods($class);
     if ($name === '') {
         throw new MemberAccessException("Cannot read a class '{$class}' property without name.");
     } elseif (isset($methods[$m = 'get' . $uname]) || isset($methods[$m = 'is' . $uname])) {
         // property getter
         if ($methods[$m] === 0) {
             $rm = new \ReflectionMethod($class, $m);
             $methods[$m] = $rm->returnsReference();
         }
         if ($methods[$m] === TRUE) {
             return $_this->{$m}();
         } else {
             $val = $_this->{$m}();
             return $val;
         }
     } elseif (isset($methods[$name])) {
         // public method as closure getter
         $val = Callback::closure($_this, $name);
         return $val;
     } elseif (isset($methods['set' . $uname])) {
         // strict class
         throw new MemberAccessException("Cannot read a write-only property {$class}::\${$name}.");
     } else {
         // strict class
         $hint = self::getSuggestion(array_merge(array_keys(get_class_vars($class)), self::parseFullDoc($class, '~^[ \\t*]*@property(?:-read)?[ \\t]+(?:\\S+[ \\t]+)??\\$(\\w+)~m')), $name);
         throw new MemberAccessException("Cannot read an undeclared property {$class}::\${$name}" . ($hint ? ", did you mean \${$hint}?" : '.'));
     }
 }
 private function assertSameMethodAttributes(\ReflectionMethod $original, ReflectionMethod $stubbed)
 {
     $this->assertSame(array_map(function (\ReflectionParameter $parameter) {
         return $parameter->getDeclaringFunction()->getName() . '.' . $parameter->getName();
     }, $original->getParameters()), array_map(function (ReflectionParameter $parameter) {
         return $parameter->getDeclaringFunction()->getName() . '.' . $parameter->getName();
     }, $stubbed->getParameters()));
     foreach ($original->getParameters() as $parameter) {
         $this->assertSameParameterAttributes($parameter, $stubbed->getParameter($parameter->getName()));
     }
     $this->assertSame($original->isPublic(), $stubbed->isPublic());
     $this->assertSame($original->isPrivate(), $stubbed->isPrivate());
     $this->assertSame($original->isProtected(), $stubbed->isProtected());
     $this->assertSame($original->returnsReference(), $stubbed->returnsReference());
     $this->assertSame($original->isStatic(), $stubbed->isStatic());
     $this->assertSame($original->isFinal(), $stubbed->isFinal());
 }
Example #19
0
 /**
  * __get() implementation.
  * @param  object
  * @param  string  property name
  * @return mixed   property value
  * @throws MemberAccessException if the property is not defined.
  */
 public static function &get($_this, $name)
 {
     $class = get_class($_this);
     $uname = ucfirst($name);
     if (!isset(self::$methods[$class])) {
         self::$methods[$class] = array_flip(get_class_methods($class));
         // public (static and non-static) methods
     }
     if ($name === '') {
         throw new MemberAccessException("Cannot read a class '{$class}' property without name.");
     } elseif (isset(self::$methods[$class][$m = 'get' . $uname]) || isset(self::$methods[$class][$m = 'is' . $uname])) {
         // property getter
         $isRef =& self::$methods[$class][$m];
         if (!is_bool($isRef)) {
             $rm = new \ReflectionMethod($class, $m);
             $isRef = $rm->returnsReference();
         }
         if ($isRef) {
             return $_this->{$m}();
         } else {
             $val = $_this->{$m}();
             return $val;
         }
     } elseif (isset(self::$methods[$class][$name])) {
         // public method as closure getter
         $val = Callback::create($_this, $name);
         return $val;
     } else {
         // strict class
         $type = isset(self::$methods[$class]['set' . $uname]) ? 'a write-only' : 'an undeclared';
         throw new MemberAccessException("Cannot read {$type} property {$class}::\${$name}.");
     }
 }
Example #20
0
 /**
  * Creates the implementation of a single method
  *
  * @param ReflectionMethod $method
  *
  * @return string
  */
 protected function implementMethod(ReflectionMethod $method, $static = false)
 {
     $modifiers = implode(' ', Reflection::getModifierNames($method->getModifiers() & ~ReflectionMethod::IS_ABSTRACT));
     $reference = $method->returnsReference() ? '&' : '';
     if ($static) {
         $context = '__CLASS__';
     } else {
         $context = '$this';
     }
     $docComment = $method->getDocComment() ?: '';
     $methodDef = "\n\t{$docComment}\n\t{$modifiers} function {$reference}{$method->getName()}({$this->generateMethodParameters($method)})\n\t{\n\t\t\$__PHAKE_args = array();\n\t\t{$this->copyMethodParameters($method)}\n\n        \$__PHAKE_info = Phake::getInfo({$context});\n\t\tif (\$__PHAKE_info === null) {\n\t\t    return null;\n\t\t}\n\n\t\t\$__PHAKE_funcArgs = func_get_args();\n\t\t\$__PHAKE_answer = \$__PHAKE_info->getHandlerChain()->invoke({$context}, '{$method->getName()}', \$__PHAKE_funcArgs, \$__PHAKE_args);\n\n\t    \$__PHAKE_callback = \$__PHAKE_answer->getAnswerCallback({$context}, '{$method->getName()}');\n\t    \$__PHAKE_result = call_user_func_array(\$__PHAKE_callback, \$__PHAKE_args);\n\t    \$__PHAKE_answer->processAnswer(\$__PHAKE_result);\n\t    return \$__PHAKE_result;\n\t}\n";
     return $methodDef;
 }
Example #21
0
 protected function generateMethodDefinitionFromExisting(ReflectionMethod $method)
 {
     if ($method->isPrivate()) {
         $modifier = 'private';
     } else {
         if ($method->isProtected()) {
             $modifier = 'protected';
         } else {
             $modifier = 'public';
         }
     }
     if ($method->isStatic()) {
         $modifier .= ' static';
     }
     if ($method->returnsReference()) {
         $reference = '&';
     } else {
         $reference = '';
     }
     return $this->generateMethodDefinition($method->getDeclaringClass()->getName(), $method->getName(), $modifier, $reference, PHPUnit_Util_Class::getMethodParameters($method));
 }
 /**
  * @param  string           $templateDir
  * @param  ReflectionMethod $method
  * @return string
  */
 protected static function generateMockedMethodDefinitionFromExisting($templateDir, ReflectionMethod $method)
 {
     if ($method->isPrivate()) {
         $modifier = 'private';
     } else {
         if ($method->isProtected()) {
             $modifier = 'protected';
         } else {
             $modifier = 'public';
         }
     }
     if ($method->isStatic()) {
         $static = TRUE;
     } else {
         $static = FALSE;
     }
     if ($method->returnsReference()) {
         $reference = '&';
     } else {
         $reference = '';
     }
     return self::generateMockedMethodDefinition($templateDir, $method->getDeclaringClass()->getName(), $method->getName(), $modifier, PHPUnit_Util_Class::getMethodParameters($method), PHPUnit_Util_Class::getMethodParameters($method, TRUE), $reference, $static);
 }
Example #23
0
function methodData(ReflectionMethod $method)
{
    $details = "";
    $name = $method->getName();
    if ($method->isUserDefined()) {
        $details .= "{$name} -- метод определён пользователем<br>";
    }
    if ($method->isInternal()) {
        $details .= "{$name} -- внутренний метод<br>";
    }
    if ($method->isAbstract()) {
        $details .= "{$name} -- абстрактный метод<br>";
    }
    if ($method->isPublic()) {
        $details .= "{$name} -- публичный метод<br>";
    }
    if ($method->isProtected()) {
        $details .= "{$name} -- защищенный метод<br>";
    }
    if ($method->isPrivate()) {
        $details .= "{$name} -- закрытый метод метод<br>";
    }
    if ($method->isStatic()) {
        $details .= "{$name} -- статичный метод<br>";
    }
    if ($method->isFinal()) {
        $details .= "{$name} -- финальный метод<br>";
    }
    if ($method->isConstructor()) {
        $details .= "{$name} -- метод конструктора<br>";
    }
    if ($method->returnsReference()) {
        $details .= "{$name} -- метод возвращает ссылку а не значение<br>";
    }
    return $details;
}
Example #24
0
 /**
  * Returns the signature of a method.
  *
  * @param  ReflectionMethod $method
  * @return string
  * @since  Method available since Release 3.2.0
  */
 public static function getMethodSignature(ReflectionMethod $method)
 {
     if ($method->isPrivate()) {
         $modifier = 'private';
     } else {
         if ($method->isProtected()) {
             $modifier = 'protected';
         } else {
             $modifier = 'public';
         }
     }
     if ($method->isStatic()) {
         $modifier .= ' static';
     }
     if ($method->returnsReference()) {
         $reference = '&';
     } else {
         $reference = '';
     }
     return sprintf('%s function %s%s(%s)', $modifier, $reference, $method->getName(), self::getMethodParameters($method));
 }
Example #25
0
 /**
  * Generates the definition of a method to be proxied.
  *
  * @param string            $templateDir Location of the templates to be used to create the proxy.
  * @param \ReflectionMethod $method      Name of the method to be reflected.
  *
  * @return array Information about the method to be proxied.
  */
 protected static function generateProxiedMethodDefinition($templateDir, \ReflectionMethod $method)
 {
     $reference = '';
     if ($method->returnsReference()) {
         $reference = '&';
     }
     $template = self::createTemplateObject($templateDir . 'proxied_method.tpl');
     $template->setVar(array('arguments_declaration' => self::getArgumentDeclaration($method), 'arguments' => self::getMethodCallParameters($method), 'method_name' => $method->getName(), 'reference' => $reference));
     return $template->render();
 }
Example #26
0
 /**
  * Replace abstract protected methods (the only enforceable type outside
  * of public methods). The replacement is just a stub that does nothing.
  */
 protected static function _replaceProtectedAbstractMethod(ReflectionMethod $method)
 {
     $name = $method->getName();
     if (self::_isReservedWord($name)) {
         return " /* Could not replace {$name}() as it is a reserved word */ ";
     }
     $body = '';
     $methodParams = array();
     $params = $method->getParameters();
     foreach ($params as $param) {
         $paramDef = '';
         if ($param->isArray()) {
             $paramDef .= 'array ';
         } elseif ($param->getClass()) {
             $paramDef .= $param->getClass()->getName() . ' ';
         }
         $paramDef .= ($param->isPassedByReference() ? '&' : '') . '$' . $param->getName();
         if ($param->isDefaultValueAvailable()) {
             $default = var_export($param->getDefaultValue(), true);
             if ($default == '') {
                 $default = 'null';
             }
             $paramDef .= ' = ' . $default;
         } else {
             if ($param->isOptional()) {
                 $paramDef .= ' = null';
             }
         }
         $methodParams[] = $paramDef;
     }
     $paramDef = implode(',', $methodParams);
     $access = 'protected';
     $returnByRef = $method->returnsReference() ? ' & ' : '';
     return $access . ' function ' . $returnByRef . $name . '(' . $paramDef . ')' . '{' . $body . '}';
 }
 /**
  * @param  string           $templateDir
  * @param  ReflectionMethod $method
  * @param  boolean          $cloneArguments
  * @param  boolean          $callOriginalMethods
  *
  * @return string
  */
 protected function generateMockedMethodDefinitionFromExisting($templateDir, ReflectionMethod $method, $cloneArguments, $callOriginalMethods)
 {
     if ($method->isPrivate()) {
         $modifier = 'private';
     } elseif ($method->isProtected()) {
         $modifier = 'protected';
     } else {
         $modifier = 'public';
     }
     if ($method->isStatic()) {
         $modifier .= ' static';
     }
     if ($method->returnsReference()) {
         $reference = '&';
     } else {
         $reference = '';
     }
     return $this->generateMockedMethodDefinition($templateDir, $method->getDeclaringClass()->getName(), $method->getName(), $cloneArguments, $modifier, $this->getMethodParameters($method), $this->getMethodParameters($method, true), $reference, $callOriginalMethods, $method->isStatic());
 }
Example #28
0
 /**
  * __get() implementation.
  * @param  object
  * @param  string  property name
  * @return mixed   property value
  * @throws MemberAccessException if the property is not defined.
  */
 public static function &get($_this, $name)
 {
     $class = get_class($_this);
     $uname = ucfirst($name);
     $methods =& self::getMethods($class);
     if ($name === '') {
         throw new MemberAccessException("Cannot read a class '{$class}' property without name.");
     } elseif (isset($methods[$m = 'get' . $uname]) || isset($methods[$m = 'is' . $uname])) {
         // property getter
         if ($methods[$m] === 0) {
             $rm = new \ReflectionMethod($class, $m);
             $methods[$m] = $rm->returnsReference();
         }
         if ($methods[$m] === TRUE) {
             return $_this->{$m}();
         } else {
             $val = $_this->{$m}();
             return $val;
         }
     } elseif (isset($methods[$name])) {
         // public method as closure getter
         if (preg_match('#^(is|get|has)([A-Z]|$)#', $name) && ($rm = new \ReflectionMethod($class, $name)) && !$rm->getNumberOfRequiredParameters()) {
             $source = '';
             foreach (debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : FALSE) as $item) {
                 if (isset($item['file']) && dirname($item['file']) !== __DIR__) {
                     $source = " in {$item['file']}:{$item['line']}";
                     break;
                 }
             }
             trigger_error("Did you forgot parentheses after {$name}{$source}?", E_USER_WARNING);
         }
         $val = Callback::closure($_this, $name);
         return $val;
     } elseif (isset($methods['set' . $uname])) {
         // strict class
         throw new MemberAccessException("Cannot read a write-only property {$class}::\${$name}.");
     } else {
         // strict class
         $hint = self::getSuggestion(array_merge(array_keys(get_class_vars($class)), self::parseFullDoc($class, '~^[ \\t*]*@property(?:-read)?[ \\t]+(?:\\S+[ \\t]+)??\\$(\\w+)~m')), $name);
         throw new MemberAccessException("Cannot read an undeclared property {$class}::\${$name}" . ($hint ? ", did you mean \${$hint}?" : '.'));
     }
 }
Example #29
0
 /**
  * Creates the implementation of a single method
  *
  * @param ReflectionMethod $method
  *
  * @return string
  */
 protected function implementMethod(ReflectionMethod $method)
 {
     $modifiers = implode(' ', Reflection::getModifierNames($method->getModifiers() & ~ReflectionMethod::IS_ABSTRACT));
     $reference = $method->returnsReference() ? '&' : '';
     $methodDef = "\n\t{$modifiers} function {$reference}{$method->getName()}({$this->generateMethodParameters($method)})\n\t{\n\t\t\$args = array();\n\t\t{$this->copyMethodParameters($method)}\n\t\t\n\t\t\$funcArgs = func_get_args();\n\t\t\$answer = \$this->__PHAKE_handlerChain->invoke(\$this, '{$method->getName()}', \$funcArgs, \$args);\n\t\t\n\t\tif (\$answer instanceof Phake_Stubber_Answers_IDelegator)\n\t\t{\n\t\t\t\$delegate = \$answer->getAnswer();\n\t\t\t\$callback = \$delegate->getCallBack(\$this, '{$method->getName()}', \$args);\n\t\t\t\$arguments = \$delegate->getArguments('{$method->getName()}', \$args);\n\n\t\t\t\$realAnswer = call_user_func_array(\$callback, \$arguments);\n\t\t\t\$answer->processAnswer(\$realAnswer);\n\t\t\treturn \$realAnswer;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t\$returnAnswer = \$answer->getAnswer();\n\t\t\treturn \$returnAnswer;\n\t\t}\n\t}\n";
     return $methodDef;
 }
<?php

class TestClass
{
    public function &foo()
    {
    }
    private function bar()
    {
    }
}
$methodInfo = new ReflectionMethod('TestClass::foo');
var_dump($methodInfo->returnsReference());
$methodInfo = new ReflectionMethod('TestClass::bar');
var_dump($methodInfo->returnsReference());