/** * @param \EBT\ExtensionBuilder\Domain\Model\ClassObject\ClassObject $classObject * @param bool $skipStatements * @return \PhpParser\Node\Stmt\Class_ */ public function buildClassNode($classObject, $skipStatements = false) { $factory = new \PhpParser\BuilderFactory(); $classNodeBuilder = $factory->class($classObject->getName()); if ($classObject->getParentClassName()) { $classNodeBuilder->extend(self::buildNodeFromName($classObject->getParentClassName())); } $interfaceNames = $classObject->getInterfaceNames(); if (count($interfaceNames) > 0) { call_user_func_array(array($classNodeBuilder, 'implement'), $interfaceNames); } if (!$skipStatements) { $stmts = array(); $properties = array(); $methods = array(); foreach ($classObject->getUseTraitStatement() as $statement) { $stmts[] = $statement; } foreach ($classObject->getMethods() as $method) { $methods[$method->getName()] = $this->buildMethodNode($method); } foreach ($classObject->getProperties() as $property) { $properties[$property->getName()] = $this->buildPropertyNode($property); } $constants = $classObject->getConstants(); if (is_array($constants)) { foreach ($constants as $name => $value) { $stmts[] = self::buildClassConstantNode($name, $value); } } foreach ($properties as $property) { $stmts[] = $property; } foreach ($methods as $method) { $stmts[] = $method; } $classNodeBuilder->addStmts($stmts); } $classNode = $classNodeBuilder->getNode(); $classNode->type = $classObject->getModifiers(); $this->addCommentAttributes($classObject, $classNode); return $classNode; }
/** * Not used right now * TODO: Needs better implementation * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject * @return void */ public function sortMethods($domainObject) { $objectProperties = $domainObject->getProperties(); $sortedProperties = array(); $propertyRelatedMethods = array(); $customMethods = array(); // sort all properties and methods according to domainObject sort order foreach ($objectProperties as $objectProperty) { if ($this->classObject->propertyExists($objectProperty->getName())) { $sortedProperties[$objectProperty->getName()] = $this->classObject->getProperty($objectProperty->getName()); $methodPrefixes = array('get', 'set', 'add', 'remove', 'is'); foreach ($methodPrefixes as $methodPrefix) { $methodName = self::getMethodName($objectProperty, $methodPrefix); if ($this->classObject->methodExists($methodName)) { $propertyRelatedMethods[$methodName] = $this->classObject->getMethod($methodName); } } } } // add the properties that were not in the domainObject $classProperties = $this->classObject->getProperties(); $sortedPropertyNames = array_keys($sortedProperties); foreach ($classProperties as $classProperty) { if (!in_array($classProperty->getName(), $sortedProperties)) { $sortedProperties[$classProperty->getName()] = $classProperty; } } // add custom methods that were manually added to the class $classMethods = $this->classObject->getMethods(); $propertyRelatedMethodNames = array_keys($propertyRelatedMethods); foreach ($classMethods as $classMethod) { if (!in_array($classMethod->getName(), $propertyRelatedMethodNames)) { $customMethods[$classMethod->getName()] = $classMethod; } } $sortedMethods = array_merge($customMethods, $propertyRelatedMethods); $this->classObject->setProperties($sortedProperties); $this->classObject->setMethods($sortedMethods); }
/** * compares the number of methods found by parsing * with those retrieved from the reflection class * * @param \EBT\ExtensionBuilder\Domain\Model\ClassObject\ClassObject $classObject * @param \TYPO3\CMS\Extbase\Reflection\ClassReflection $classReflection * @return void */ public function ParserFindsAllMethods($classObject, $classReflection) { $reflectionMethodCount = count($classReflection->getMethods()); $classObjectMethodCount = count($classObject->getMethods()); $this->assertEquals($classObjectMethodCount, $reflectionMethodCount, 'Not all Methods were found!: ' . $reflectionMethodCount); }
/** * * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject\AbstractProperty $oldProperty * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject\AbstractProperty $newProperty * * @return void */ protected function updatePropertyRelatedMethods($oldProperty, $newProperty) { if ($newProperty->isAnyToManyRelation()) { $this->updateMethod($oldProperty, $newProperty, 'add'); $this->updateMethod($oldProperty, $newProperty, 'remove'); } $this->updateMethod($oldProperty, $newProperty, 'get'); $this->updateMethod($oldProperty, $newProperty, 'set'); if ($newProperty->isBoolean()) { $this->updateMethod($oldProperty, $newProperty, 'is'); } if ($newProperty->getTypeForComment() != $this->updateExtensionKey($oldProperty->getTypeForComment())) { if ($oldProperty->isBoolean() && !$newProperty->isBoolean()) { $this->classObject->removeMethod(ClassBuilder::getMethodName($oldProperty, 'is')); $this->log('Method removed:' . ClassBuilder::getMethodName($oldProperty, 'is'), 1, $this->classObject->getMethods()); } } }