public function testNewClass() { $class = new GClass(); $class->setName('tiptoi\\Entities\\Page'); // da dieser Test in keinem NS ist $this->assertEquals('\\tiptoi\\Entities', $class->getNamespace()); $this->assertEquals('Page', $class->getClassName()); $class->addMethod(GMethod::factory('addOID', array(GParameter::factory('$oid', GClass::factory('OID'))), 'if (!$this->oids->contains($oid)) { $this->oids->add($oid); } return $this; ')); $class->setMethod(GMethod::factory('removeOID', array(GParameter::factory('$oid', GClass::factory('OID'))), 'if ($this->oids->contains($oid)) { $this->oids->removeElement($oid); } return $this; ')); $class->addMethod(GMethod::factory('getOIDs', array(), 'return $this->oids;')); $classCode = <<<'CLASS_CODE' class Page { public function addOID(OID $oid) { if (!$this->oids->contains($oid)) { $this->oids->add($oid); } return $this; } public function removeOID(OID $oid) { if ($this->oids->contains($oid)) { $this->oids->removeElement($oid); } return $this; } public function getOIDs() { return $this->oids; } } CLASS_CODE; // file_put_contents('D:\fixture.txt', $classCode); // file_put_contents('D:\compiled.txt',$class->php()); $this->assertEquals($classCode, $class->php()); }
public function elevate(Reflector $reflector) { $this->reflector = $reflector; $this->elevateValues(array('name', 'getShortName'), 'startLine', 'endLine', array('srcFileName', 'getFileName'), array('returnsReference', 'returnsReference'), array('namespace', 'getNamespaceName')); foreach ($this->reflector->getParameters() as $rParameter) { try { $this->parameters[] = GParameter::reflectorFactory($rParameter); } catch (ReflectionException $e) { // das ist nicht die von php $e->appendMessage("\n" . 'Parameter in Methode/Function: ' . $this->name); throw $e; } } }
public function testHintClassNotAutoloadable() { $param = new GParameter('justAParam'); $param->setHint('Cls\\Does\\Not\\Exists'); $this->assertInstanceOf('Psc\\Code\\Generate\\GClass', $param->getHint()); }
public function generatePropertiesConstructor(array $properties) { if ($this->class->hasOwnMethod('__construct')) { $constructor = $this->class->getMethod('__construct'); //if (count($constructor->getBodyCode()) > 0) { // throw new \Psc\Exception('Ein constructor für '.$this->class->getFQN().' ist bereits definiert und hat einen Body. Deshalb kann er nicht überschrieben werden'); // ändere dies, falls das mal anders sein soll - dunno (yagni) //} } else { $constructor = $this->class->createMethod('__construct'); $this->class->setMethodOrder($constructor, GClass::PREPEND); } $code = array(); foreach ($properties as $property) { if (is_array($property) && array_key_exists('property', $property)) { $argument = $property; $property = $argument['property']; } else { $argument = array(); } if (!$property instanceof ClassBuilderProperty) { throw new \InvalidArgumentException('Es können nur ClassBuilderProperties übergeben werden'); } $param = new GParameter($property->getName(), $property->getPHPHint()); if (array_key_exists('default', $argument)) { $param->setDefault($argument['default']); $codeName = 'constructorSetterPartOptional'; } else { $codeName = 'constructorSetterPart'; } $code = array_merge($code, $this->createCode($codeName, array('setter' => $property->getSetterName(), 'property' => $property->getName()))); $constructor->addParameter($param); } $constructor->appendBodyLines($code); return $this; }
protected function resolveTaskDependency(GParameter $parameter, GClass $gClass) { // zuerst nach Name switch ($parameter->getName()) { case 'targetProject': case 'sourceProject': case 'target': case 'hostName': case 'vhostName': case 'baseUrl': case 'changelog': case 'webforgeContainer': $getter = 'get' . ucfirst($parameter->getName()); return $this->{$getter}(); default: if (!$parameter->isOptional()) { throw new \Psc\Exception('Dependency in ' . $gClass->getFQN() . ' ' . $parameter->getName() . ' kann nicht gelöst werden.'); } else { return $parameter->getDefault(); } } }