public function testFunctions() { $result = $this->parser->parse('<?php function add($a, $b) { return $a + $b; } function sub($a, $b) { return $a - $b; }'); $this->assertCount(2, $result->getFunctions()); $this->assertTrue($result->hasFunction('add')); $func = $result->getFunction('add'); $this->assertInstanceOf(PHPFunction::class, $func); $expectedSource = <<<'CODE' function add($a, $b) { return $a + $b; } CODE; $this->assertSourceCode($expectedSource, $func->getSource()); $this->assertTrue($result->hasFunction('sub')); $func = $result->getFunction('sub'); $this->assertInstanceOf(PHPFunction::class, $func); $expectedSource = <<<'CODE' function sub($a, $b) { return $a - $b; } CODE; $this->assertSourceCode($expectedSource, $func->getSource()); }
/** * @param string $contents * @param CommandParams $params * @param string $fileName * * @return string * * @throws ExecutionFailedException */ private function findFunction($contents, CommandParams $params, $fileName) { $functionName = $params->getArgument(2); $result = $this->parser->parse($contents); if (!$result->hasFunction($functionName)) { throw new ExecutionFailedException("Function '{$functionName}' was not found in file '{$fileName}'."); } return $this->wrapLines($result->getFunction($functionName)->getSource()); }