/** * @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 properties 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 ParserFindsAllProperties($classObject, $classReflection) { $reflectionPropertyCount = count($classReflection->getProperties()); $classObjectPropertCount = count($classObject->getProperties()); $this->assertEquals($classObjectPropertCount, $reflectionPropertyCount, 'Not all Properties were found!'); }
/** * update all relevant namespace parts in tags, typehints etc. */ protected function updateVendorName() { $this->classObject->setNamespaceName($this->renameVendor($this->classObject->getNamespaceName())); foreach ($this->classObject->getProperties() as $property) { foreach ($property->getTags() as $tagName => $tagValue) { if (is_array($tagValue)) { $tagValue = $tagValue[0]; } if (!empty($tagValue)) { $tagValue = $this->renameVendor($tagValue); $property->setTag($tagName, $tagValue, true); } } } foreach ($this->classObject->getMethods() as $method) { foreach ($method->getTags() as $tagName => $tagValue) { if (is_array($tagValue)) { $tagValue = $tagValue[0]; } if (!empty($tagValue)) { $tagValue = $this->renameVendor($tagValue); $method->setTag($tagName, $tagValue, true); } } foreach ($method->getParameters() as $parameter) { $typeHint = $parameter->getTypeHint(); if (!empty($typeHint)) { $parameter->setTypeHint($this->renameVendor($typeHint)); } $varType = $parameter->getVarType(); if (!empty($varType)) { $parameter->setVarType($this->renameVendor($varType)); } } } }