addMethod() public method

Adds a method to the class definition
public addMethod ( ClassMethod $method, array $statement = null ) : void
$method ClassMethod
$statement array
return void
Beispiel #1
0
 /**
  * Creates the property shortcuts
  *
  * @param array $property
  * @param ClassDefinition $classDefinition
  * @throws CompilerException
  */
 protected function _processShorcuts(array $property, ClassDefinition $classDefinition)
 {
     foreach ($property['shortcuts'] as $shortcut) {
         if (substr($property['name'], 0, 1) == '_') {
             $name = substr($property['name'], 1);
         } else {
             $name = $property['name'];
         }
         $docBlock = isset($shortcut['docblock']) ? $shortcut['docblock'] : isset($property['docblock']) ? $property['docblock'] : null;
         $returnsType = array();
         if ($docBlock) {
             $docBlockParser = new DocblockParser('/' . $docBlock . '/');
             $docBlockParsed = $docBlockParser->parse();
             if ($annotations = $docBlockParsed->getAnnotationsByType('var')) {
                 $returnsType = array_map(function ($type) {
                     return ($type = trim($type)) == 'mixed' ? 'variable' : $type;
                 }, (array) explode('|', $annotations[0]->getString()));
             }
             // Clear annotations
             $docBlockParsed->setAnnotations(array());
             $docBlock = $docBlockParsed->generate();
         }
         switch ($shortcut['name']) {
             case 'get':
                 $classDefinition->addMethod(new ClassMethod($classDefinition, array('public'), 'get' . Utils::camelize($name), null, new StatementsBlock(array(array('type' => 'return', 'expr' => array('type' => 'property-access', 'left' => array('type' => 'variable', 'value' => 'this'), 'right' => array('type' => 'variable', 'value' => $property['name']))))), $docBlock, $this->createReturnsType($returnsType), $shortcut), $shortcut);
                 break;
             case 'set':
                 $classDefinition->addMethod(new ClassMethod($classDefinition, array('public'), 'set' . Utils::camelize($name), new ClassMethodParameters(array(array('type' => 'parameter', 'name' => $name, 'const' => 0, 'data-type' => count($returnsType) == 1 ? $returnsType[0] : 'variable', 'mandatory' => 0))), new StatementsBlock(array(array('type' => 'let', 'assignments' => array(array('assign-type' => 'object-property', 'operator' => 'assign', 'variable' => 'this', 'property' => $property['name'], 'expr' => array('type' => 'variable', 'value' => $name, 'file' => $property['file'], 'line' => $property['line'], 'char' => $property['char']), 'file' => $property['file'], 'line' => $property['line'], 'char' => $property['char']))))), $docBlock, null, $shortcut), $shortcut);
                 break;
             case 'toString':
             case '__toString':
                 $classDefinition->addMethod(new ClassMethod($classDefinition, array('public'), '__toString', null, new StatementsBlock(array(array('type' => 'return', 'expr' => array('type' => 'property-access', 'left' => array('type' => 'variable', 'value' => 'this'), 'right' => array('type' => 'variable', 'value' => $property['name']))))), $docBlock, $this->createReturnsType(array('string')), $shortcut), $shortcut);
                 break;
             default:
                 throw new CompilerException("Unknown shortcut '" . $shortcut['name'] . "'", $shortcut);
         }
     }
 }
Beispiel #2
0
 /**
  * Builds a class definition from reflection
  *
  * @param \ReflectionClass $class
  *
  * @return ClassDefinition
  */
 public static function buildFromReflection(\ReflectionClass $class)
 {
     $classDefinition = new ClassDefinition($class->getNamespaceName(), $class->getName());
     $methods = $class->getMethods();
     if (count($methods) > 0) {
         foreach ($methods as $method) {
             $parameters = array();
             foreach ($method->getParameters() as $row) {
                 $params = array('type' => 'parameter', 'name' => $row->getName(), 'const' => 0, 'data-type' => 'variable', 'mandatory' => !$row->isOptional());
                 if (!$params['mandatory']) {
                     try {
                         $params['default'] = $row->getDefaultValue();
                     } catch (\ReflectionException $e) {
                         // TODO: dummy default value
                         $params['default'] = true;
                     }
                 }
                 $parameters[] = $params;
             }
             $classMethod = new ClassMethod($classDefinition, array(), $method->getName(), new ClassMethodParameters($parameters));
             $classMethod->setIsStatic($method->isStatic());
             $classMethod->setIsBundled(true);
             $classDefinition->addMethod($classMethod);
         }
     }
     $constants = $class->getConstants();
     if (count($constants) > 0) {
         foreach ($constants as $constantName => $constantValue) {
             $type = self::_convertPhpConstantType(gettype($constantValue));
             $classConstant = new ClassConstant($constantName, array('value' => $constantValue, 'type' => $type), null);
             $classDefinition->addConstant($classConstant);
         }
     }
     $properties = $class->getProperties();
     if (count($properties) > 0) {
         foreach ($properties as $property) {
             $visibility = array();
             if ($property->isPublic()) {
                 $visibility[] = 'public';
             }
             if ($property->isPrivate()) {
                 $visibility[] = 'private';
             }
             if ($property->isProtected()) {
                 $visibility[] = 'protected';
             }
             if ($property->isStatic()) {
                 $visibility[] = 'static';
             }
             $classProperty = new ClassProperty($classDefinition, $visibility, $property->getName(), null, null, null);
             $classDefinition->addProperty($classProperty);
         }
     }
     $classDefinition->setIsBundled(true);
     return $classDefinition;
 }