public function testVisitFunction() { $writer = new Writer(); $function = new PhpFunction('foo'); $function->addParameter(PhpParameter::create('a'))->addParameter(PhpParameter::create('b'))->setBody($writer->writeln('if ($a === $b) {')->indent()->writeln('throw new \\InvalidArgumentException(\'$a is not allowed to be the same as $b.\');')->outdent()->write("}\n\n")->write('return $b;')->getContent()); $visitor = new DefaultVisitor(); $visitor->visitFunction($function); $this->assertEquals($this->getContent('a_b_function.php'), $visitor->getContent()); }
public function testReturnType() { $expected = "function foo(): int {\n}\n"; $generator = new ModelGenerator(['generateReturnTypeHints' => true, 'generateDocblock' => false]); $method = PhpFunction::create('foo')->setType('int'); $this->assertEquals($expected, $generator->generate($method)); }
public function testStrictTypesDeclaration() { $expected = "<?php\ndeclare(strict_types=1);\n\nfunction fn(\$a) {\n}\n"; $fn = PhpFunction::create('fn')->addParameter(PhpParameter::create('a')); $codegen = new CodeFileGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false, 'declareStrictTypes' => true]); $code = $codegen->generate($fn); $this->assertEquals($expected, $code); }
public function testSimpleDescParameter() { $function = new PhpFunction(); $function->addSimpleDescParameter('param1', 'string'); $this->assertFalse($function->hasParameter('param2')); $param1 = $function->getParameter('param1'); $this->assertEquals('string', $param1->getType()); $this->assertFalse($param1->hasValue()); $function->addSimpleDescParameter('param2', 'string', 'desc'); $this->assertTrue($function->hasParameter('param2')); $param2 = $function->getParameter('param2'); $this->assertEquals('string', $param2->getType()); $this->assertFalse($param2->hasValue()); $function->addSimpleDescParameter('param3', 'string', 'desc', null); $this->assertTrue($function->hasParameter('param3')); $param3 = $function->getParameter('param3'); $this->assertEquals('string', $param3->getType()); $this->assertNull($param3->getValue()); }
public function testFromReflection() { $doc = new Docblock('/** * Makes foo with bar * * @param string $baz * @return string */'); $function = new PhpFunction('wurst'); $function->addParameter(PhpParameter::create('baz')->setDefaultValue(null)->setType('string'))->setBody('return \'wurst\';')->setDocblock($doc)->setDescription($doc->getShortDescription())->setLongDescription($doc->getLongDescription()); $this->assertEquals($function, PhpFunction::fromReflection(new \ReflectionFunction('wurst'))); }
public static function fromReflection(\ReflectionFunction $ref) { $function = PhpFunction::create($ref->name)->setReferenceReturned($ref->returnsReference())->setBody(ReflectionUtils::getFunctionBody($ref)); $docblock = new Docblock($ref); $function->setDocblock($docblock); $function->setDescription($docblock->getShortDescription()); $function->setLongDescription($docblock->getLongDescription()); foreach ($ref->getParameters() as $refParam) { assert($refParam instanceof \ReflectionParameter); // hmm - assert here? $param = PhpParameter::fromReflection($refParam); $function->addParameter($param); } return $function; }
public function testObjectParam() { $expected = '/** * @param Request $r * @param mixed $a * @return Response this method returns a response object */'; $function = PhpFunction::create(self::METHOD)->setType('Response', 'this method returns a response object')->addParameter(PhpParameter::create('r')->setType('Request'))->addParameter(PhpParameter::create('a')->setType('mixed')); $function->generateDocblock(); $this->assertSame($expected, $function->getDocblock()->toString()); }
public function visitFunction(PhpFunction $function) { if ($namespace = $function->getNamespace()) { $this->writer->write("namespace {$namespace};\n\n"); } $this->visitDocblock($function->getDocblock()); $this->writer->write("function {$function->getName()}("); $this->writeParameters($function->getParameters()); $this->writer->write(')'); $this->writeFunctionReturnType($function->getType()); $this->writer->write(" {\n")->indent()->writeln(trim($function->getBody()))->outdent()->rtrim()->write('}'); }