/** * Sets a method. * * @param Stagehand_PHP_Class_Method $method */ public function setMethod(Stagehand_PHP_Class_Method $method) { $this->_methods[$method->getName()] = $method; }
/** * Creates a partial code for class method. * ` @param Stagehand_PHP_Class_Method $method * @return string */ protected function _createMethodCode($method) { if ($method->isFinal()) { return; } return $method->render(); }
/** * class_statement_3 * method_modifiers function is_reference T_STRING '(' parameter_list ')' method_body */ protected function class_statement_3($params) { if (is_array($params[0])) { $methodModifiers = $params[0]; } else { $methodModifiers = array($params[0]); } $isReference = $params[2] ? true : false; $methodName = $params[3]->getValue(); $parameterList = $params[5]; $methodBody = $params[7] ? $params[7] : null; $method = new Stagehand_PHP_Class_Method($methodName); $method->setCode($methodBody); $lex = $this->getParser()->lex; $docComment = $lex->getLatestDocComment(); if ($docComment) { $method->setDocComment($docComment, true); } if ($isReference) { $method->setReference(); } if ($parameterList) { foreach ($parameterList as $argument) { $method->addArgument($argument); } } $this->_setModifiers($method, $methodModifiers); $this->addCurrentMethod($method); return parent::execute(__FUNCTION__, $params); }
/** * @test */ public function defineInterfaceAndUse() { $className = 'ExampleForInterface'; $class = new Stagehand_PHP_Class($className); $class->defineInterface(); $propertyA = new Stagehand_PHP_Class_Property('a'); $methodB = new Stagehand_PHP_Class_Method('b'); $methodC = new Stagehand_PHP_Class_Method('c'); $methodC->defineAbstract(); $methodC->defineStatic(); $methodD = new Stagehand_PHP_Class_Method('d'); $methodD->defineProtected(); $methodE = new Stagehand_PHP_Class_Method('e'); $methodE->definePrivate(); $class->addProperty($propertyA); $class->addMethod($methodB); $class->addMethod($methodC); $class->addMethod($methodD); $class->addMethod($methodE); $class->load(); $refClass = new ReflectionClass($className); $this->assertEquals($refClass->getName(), $className); $this->assertTrue($refClass->isInterface()); $this->assertFalse($refClass->hasProperty('a')); $this->assertTrue($refClass->hasMethod('b')); $this->assertTrue($refClass->hasMethod('c')); $this->assertFalse($refClass->hasMethod('d')); $this->assertFalse($refClass->hasMethod('e')); $refMethodB = $refClass->getMethod('b'); $refMethodC = $refClass->getMethod('c'); $this->assertTrue($refMethodB->isPublic()); $this->assertTrue($refMethodB->isAbstract()); $this->assertFalse($refMethodB->isStatic()); $this->assertTrue($refMethodC->isPublic()); $this->assertTrue($refMethodC->isAbstract()); $this->assertTrue($refMethodC->isStatic()); require_once dirname(__FILE__) . "/ClassTest/ImplementedClass.php"; $instance = new ImplementedClass(); $this->assertEquals($instance->b(), 'foo'); $this->assertEquals(ImplementedClass::c(), 'bar'); }
/** * A function for test of code generator. * * @param string $name class name */ function define_class_for_code_genration_test() { $className = 'ExampleForCodeGenerationTest'; $class = new Stagehand_PHP_Class($className); $publicProperty = new Stagehand_PHP_Class_Property('a'); $protectedProperty = new Stagehand_PHP_Class_Property('b', 100); $privateProperty = new Stagehand_PHP_Class_Property('c', array(1, 3, 5)); $staticProperty = new Stagehand_PHP_Class_Property('d', 'static'); $protectedProperty->defineProtected(); $privateProperty->definePrivate(); $staticProperty->defineStatic(); $argumentA = new Stagehand_PHP_Class_Method_Argument('a'); $argumentB = new Stagehand_PHP_Class_Method_Argument('b'); $argumentC = new Stagehand_PHP_Class_Method_Argument('c'); $argumentD = new Stagehand_PHP_Class_Method_Argument('d'); $argumentE = new Stagehand_PHP_Class_Method_Argument('e'); $argumentF = new Stagehand_PHP_Class_Method_Argument('f'); $argumentB->setTypeHinting('array'); $argumentC->setRequirement(false); $argumentD->setRequirement(false); $argumentD->setValue('d'); $argumentE->setRequirement(false); $argumentE->setValue(array(2, 4, 6)); $argumentF->setReference(); $publicMethod = new Stagehand_PHP_Class_Method('foo'); $protectedMethod = new Stagehand_PHP_Class_Method('bar'); $privateMethod = new Stagehand_PHP_Class_Method('baz'); $staticMethod = new Stagehand_PHP_Class_Method('qux'); $abstractMethod = new Stagehand_PHP_Class_Method('quux'); $abstractStaticMethod = new Stagehand_PHP_Class_Method('corge'); $finalMethod = new Stagehand_PHP_Class_Method('grault'); $finalStaticMethod = new Stagehand_PHP_Class_Method('garply'); $referenceMethod = new Stagehand_PHP_Class_Method('waldo'); $protectedMethod->defineProtected(); $protectedMethod->addArgument($argumentA); $protectedMethod->addArgument($argumentB); $protectedMethod->setCode('return true;'); $privateMethod->definePrivate(); $privateMethod->addArgument($argumentC); $privateMethod->addArgument($argumentD); $privateMethod->setCode('$c += 1; return $d;'); $staticMethod->defineStatic(); $staticMethod->addArgument($argumentE); $abstractMethod->defineAbstract(); $abstractStaticMethod->defineAbstract(); $abstractStaticMethod->defineStatic(); $finalMethod->defineFinal(); $finalMethod->addArgument($argumentF); $finalStaticMethod->defineFinal(); $finalStaticMethod->defineStatic(); $referenceMethod->setReference(); $constant1 = new Stagehand_PHP_Class_Constant('A'); $constant2 = new Stagehand_PHP_Class_Constant('B', 10); $constant3 = new Stagehand_PHP_Class_Constant('C', 'text constant'); $class->addProperty($publicProperty); $class->addProperty($protectedProperty); $class->addProperty($privateProperty); $class->addProperty($staticProperty); $class->addMethod($publicMethod); $class->addMethod($protectedMethod); $class->addMethod($privateMethod); $class->addMethod($staticMethod); $class->addMethod($abstractMethod); $class->addMethod($abstractStaticMethod); $class->addMethod($finalMethod); $class->addMethod($finalStaticMethod); $class->addMethod($referenceMethod); $class->addConstant($constant1); $class->addConstant($constant2); $class->addConstant($constant3); return $class; }
/** * @test */ public function useDocComment() { $name = 'getFoo'; $docComment = "A tests for DocComment.\n\n@return boolean"; $formatedDocComment = "/**\n * A tests for DocComment.\n *\n * @return boolean\n */"; $result = <<<EOF /** * A tests for DocComment. * * @return boolean */ public function getFoo() { return true; } EOF; $method = new Stagehand_PHP_Class_Method($name); $method->setCode('return true;'); $method->setDocComment($docComment); $this->assertEquals($method->getDocComment(), $formatedDocComment); $this->assertEquals($method->render(), $result); $docComment = "/**\n * A tests for DocComment.\n *\n * @return boolean\n */"; $method->setDocComment($docComment, true); $this->assertEquals($method->getDocComment(), $formatedDocComment); $this->assertEquals($method->render(), $result); }