Example #1
0
 /**
  * Retrieve parsed methods for this class
  * 
  * @return array Array of ClassMethod objects
  */
 public function getMethods()
 {
     if (null !== $this->methods) {
         return $this->methods;
     }
     $rMethods = $this->reflection->getMethods(ReflectionMethod::IS_PUBLIC);
     $methods = array();
     foreach ($rMethods as $method) {
         $methods[] = new ClassMethod($method);
     }
     $this->methods = $methods;
     return $this->methods;
 }
Example #2
0
 /**
  * fromReflection() - build a Code Generation Php Object from a Class Reflection
  *
  * @param \Zend\Reflection\ReflectionClass $reflectionClass
  * @return \Zend\CodeGenerator\Php\PhpClass
  */
 public static function fromReflection(\Zend\Reflection\ReflectionClass $reflectionClass)
 {
     $class = new self();
     $class->setSourceContent($class->getSourceContent());
     $class->setSourceDirty(false);
     if ($reflectionClass->getDocComment() != '') {
         $class->setDocblock(PhpDocblock::fromReflection($reflectionClass->getDocblock()));
     }
     $class->setAbstract($reflectionClass->isAbstract());
     // set the namespace
     if ($reflectionClass->inNamespace()) {
         $class->setNamespaceName($reflectionClass->getNamespaceName());
     }
     $class->setName($reflectionClass->getName());
     if ($parentClass = $reflectionClass->getParentClass()) {
         $class->setExtendedClass($parentClass->getName());
         $interfaces = array_diff($reflectionClass->getInterfaces(), $parentClass->getInterfaces());
     } else {
         $interfaces = $reflectionClass->getInterfaces();
     }
     $interfaceNames = array();
     foreach ($interfaces as $interface) {
         $interfaceNames[] = $interface->getName();
     }
     $class->setImplementedInterfaces($interfaceNames);
     $properties = array();
     foreach ($reflectionClass->getProperties() as $reflectionProperty) {
         if ($reflectionProperty->getDeclaringClass()->getName() == $class->getName()) {
             $properties[] = PhpProperty::fromReflection($reflectionProperty);
         }
     }
     $class->setProperties($properties);
     $methods = array();
     foreach ($reflectionClass->getMethods() as $reflectionMethod) {
         if ($reflectionMethod->getDeclaringClass()->getName() == $class->getName()) {
             $methods[] = PhpMethod::fromReflection($reflectionMethod);
         }
     }
     $class->setMethods($methods);
     return $class;
 }
Example #3
0
 /**
  * @group ZF-8307
  */
 public function testReturnClassWithNamespace()
 {
     $classReflection = new Reflection\ReflectionClass('\\ZendTest\\Reflection\\TestAsset\\TestSampleClass7');
     $paramTag = $classReflection->getMethod('doSomething')->getDocblock()->getTag('return');
     $trimOpt = Reflection\ReflectionDocblockTag::TRIM_WHITESPACE;
     $this->assertEquals('Zend\\Reflection\\Docblock', $paramTag->getType($trimOpt));
 }
Example #4
0
    /**
     * _processActionableMethods() - process all methods that can be called on this provider.
     *
     */
    protected function _processActionableMethods()
    {

        $specialtyRegex = '#(.*)(' . implode('|', $this->_specialties) . ')$#i';


        $methods = $this->_providerReflection->getMethods();

        $actionableMethods = array();
        foreach ($methods as $method) {

            $methodName = $method->getName();

            /**
             * the following will determine what methods are actually actionable
             * public, non-static, non-underscore prefixed, classes that dont
             * contain the name "
             */
            if (!$method->getDeclaringClass()->isInstantiable()
                || !$method->isPublic()
                || $methodName[0] == '_'
                || $method->isStatic()
                || in_array($methodName, array('getContextClasses', 'getName')) // other protected public methods will nee to go here
                ) {
                continue;
            }

            /**
             * check to see if the method was a required method by a Zend_Tool_* interface
             */
            foreach ($method->getDeclaringClass()->getInterfaces() as $methodDeclaringClassInterface) {
                if (strpos($methodDeclaringClassInterface->getName(), 'Zend_Tool_') === 0
                    && $methodDeclaringClassInterface->hasMethod($methodName)) {
                    continue 2;
                }
            }

            $actionableName = ucfirst($methodName);

            if (substr($actionableName, -6) == 'Action') {
                $actionableName = substr($actionableName, 0, -6);
            }

            $actionableMethods[$methodName]['methodName'] = $methodName;

            $matches = null;
            if (preg_match($specialtyRegex, $actionableName, $matches)) {
                $actionableMethods[$methodName]['actionName'] = $matches[1];
                $actionableMethods[$methodName]['specialty'] = $matches[2];
            } else {
                $actionableMethods[$methodName]['actionName'] = $actionableName;
                $actionableMethods[$methodName]['specialty'] = '_Global';
            }

            // get the action, and create non-existent actions when they dont exist (the true part below)
            $action = $this->_registry->getActionRepository()->getAction($actionableMethods[$methodName]['actionName']);
            if ($action == null) {
                $action = new \Zend\Tool\Framework\Action\Base($actionableMethods[$methodName]['actionName']);
                $this->_registry->getActionRepository()->addAction($action);
            }
            $actionableMethods[$methodName]['action'] = $action;

            if (!in_array($actionableMethods[$methodName]['action'], $this->_actions)) {
                $this->_actions[] = $actionableMethods[$methodName]['action'];
            }

            $parameterInfo = array();
            $position = 1;
            foreach ($method->getParameters() as $parameter) {
                $currentParam = $parameter->getName();
                $parameterInfo[$currentParam]['position']    = $position++;
                $parameterInfo[$currentParam]['optional']    = $parameter->isOptional();
                $parameterInfo[$currentParam]['default']     = ($parameter->isOptional()) ? $parameter->getDefaultValue() : null;
                $parameterInfo[$currentParam]['name']        = $currentParam;
                $parameterInfo[$currentParam]['type']        = 'string';
                $parameterInfo[$currentParam]['description'] = null;
            }

            $matches = null;
            if (($docComment = $method->getDocComment()) != '' &&
                (preg_match_all('/@param\s+(\w+)+\s+(\$\S+)\s+(.*?)(?=(?:\*\s*@)|(?:\*\/))/s', $docComment, $matches)))
            {
                for ($i=0; $i <= count($matches[0])-1; $i++) {
                    $currentParam = ltrim($matches[2][$i], '$');

                    if ($currentParam != '' && isset($parameterInfo[$currentParam])) {

                        $parameterInfo[$currentParam]['type'] = $matches[1][$i];

                        $descriptionSource = $matches[3][$i];

                        if ($descriptionSource != '') {
                            $parameterInfo[$currentParam]['description'] = trim($descriptionSource);
                        }

                    }

                }

            }

            $actionableMethods[$methodName]['parameterInfo'] = $parameterInfo;

        }

        $this->_actionableMethods = $actionableMethods;
    }
Example #5
0
 public function testGetDeclaringFileReturnsFilename()
 {
     $reflectionClass = new Reflection\ReflectionClass('\\ZendTest\\Reflection\\TestAsset\\TestSampleClass2');
     $this->assertContains('TestSampleClass2.php', $reflectionClass->getDeclaringFile()->getFileName());
 }
 public function testToString()
 {
     $classReflection = new Reflection\ReflectionClass('\\ZendTest\\Reflection\\TestAsset\\TestSampleClass5');
     $classDocblock = $classReflection->getDocblock();
     $expectedString = 'Docblock [ /* Docblock */ ] {' . PHP_EOL . PHP_EOL . '  - Tags [1] {' . PHP_EOL . '    Docblock Tag [ * @author ]' . PHP_EOL . '  }' . PHP_EOL . '}' . PHP_EOL;
     $this->assertEquals($expectedString, (string) $classDocblock);
 }
Example #7
0
 /**
  * Generate map of public class attributes
  *
  * @param  string $typename type name
  * @param  DOMElement $typexml target XML element
  * @return void
  */
 protected function _addClassAttributes($typename, \DOMElement $typexml)
 {
     // Do not try to autoload here because _phpTypeToAS should
     // have already attempted to load this class
     if (!class_exists($typename, false)) {
         return;
     }
     $rc = new ReflectionClass($typename);
     foreach ($rc->getProperties() as $prop) {
         if (!$prop->isPublic()) {
             continue;
         }
         $propxml = $this->_xml->createElement('property');
         $propxml->setAttribute('name', $prop->getName());
         $type = $this->_registerType($this->_getPropertyType($prop));
         $propxml->setAttribute('type', $type);
         $typexml->appendChild($propxml);
     }
 }