public function testExtract_InlineComment() { $extracter = new Extracter(); $snippet = <<<'FBODY' public function removeOID(OID $oid) { // this gets into the body if ($this->oids->contains($oid)) { // is allowed $this->oids->removeElement($oid); } // is allowed, too $indents = "correct"; return $this; } FBODY; $actual = $extracter->extractFunctionBody($snippet); $this->assertEquals(array(array(-1, '// this gets into the body'), array(4, 'if ($this->oids->contains($oid)) { // is allowed'), array(6, '$this->oids->removeElement($oid);'), array(4, '}'), array(5, '// is allowed, too'), array(4, '$indents = "correct";'), array(4, NULL), array(4, 'return $this;')), $actual); }
/** * * parsed $this->sourceCode in einen Array von Zeilen * ist sourceCode nicht gesetzt wird srcFileName nach getStartLine und getEndLine ausgeschnitten * */ public function getBodyCode() { if (!isset($this->bodyCode)) { // parse von reflection PHP-Code $this->debug = NULL; /* Hier ist die einzige Möglichkeit, wo wir die Chance haben den Indent richtig zu setzen (wenn wir von der Reflection parsen) den sonst wissen wir nie ob wir innerhalb eines Strings sind und \n einrücken dürfen */ if (!isset($this->sourceCode)) { if (isset($this->srcFileName)) { $this->sourceCode = $this->getSourceCode(new \Webforge\Common\System\File($this->srcFileName), $this->getStartLine(), $this->getEndLine()); $this->debug = "\n" . $this->srcFileName . ' ' . $this->getStartLine() . '-' . $this->getEndLine() . ' : "' . $this->sourceCode . '"' . "\n"; } else { $this->sourceCode = NULL; return $this->bodyCode = array(); } } $extracter = new Extracter(); try { $body = $extracter->extractFunctionBody($this->sourceCode); if (count($body) === 0) { $this->bodyCode = array(); //throw new \Psc\Exception('Es konnte kein Body aus '.Code::varInfo($this->sourceCode).' extrahiert werden'); } else { if ($body[0][0] === -1) { // inline comment wie dieser $this->cbraceComment = $body[0][1]; array_shift($body); } $baseIndent = max(0, $body[0][0]); foreach ($body as $key => $list) { list($indent, $line) = $list; $this->bodyCode[] = str_repeat(' ', max(0, $indent - $baseIndent)) . $line; } } } catch (\Psc\Code\ExtracterException $e) { throw new SyntaxErrorException('Kann BodyCode nicht aus SourceCode erzeugen wegen eines Parse-Errors in: ' . $e->context, 0, $e); } } return $this->bodyCode; }