Skip to content

Commit

Permalink
Guides: Remove createSpanNode from Parser
Browse files Browse the repository at this point in the history
The Parser had a method createSpanNode that did nothing more than
instantiate a SpanNode; this code could easily be moved to the invoking
locations as this does not fall within the parser's responsibilities and
only incurs dependencies.

Although the dependencies are not directly solved with this change, it
does clean up the interface and paves the way for more improvements.
  • Loading branch information
mvriel committed Mar 7, 2021
1 parent b891797 commit 09a7d5e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 30 deletions.
12 changes: 1 addition & 11 deletions src/Guides/Nodes/SpanNode.php
Expand Up @@ -22,9 +22,6 @@

class SpanNode extends Node
{
/** @var string */
protected $value;

/** @var SpanToken[] */
protected $tokens;

Expand All @@ -33,8 +30,6 @@ class SpanNode extends Node
*/
public function __construct(Environment $environment, $span)
{
parent::__construct();

if (is_array($span)) {
$span = implode("\n", $span);
}
Expand All @@ -45,15 +40,10 @@ public function __construct(Environment $environment, $span)

$spanProcessor = new SpanProcessor($environment, $span);

$this->value = $spanProcessor->process();
parent::__construct($spanProcessor->process());
$this->tokens = $spanProcessor->getTokens();
}

public function getValue() : string
{
return $this->value;
}

/**
* @return SpanToken[]
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Guides/Nodes/TableNode.php
Expand Up @@ -175,7 +175,7 @@ public function finalize(Parser $parser) : void
if ($this->lineChecker->isListLine($lines[0], false)) {
$node = $parser->parseFragment($col->getContent())->getNodes()[0];
} else {
$node = $parser->createSpanNode($col->getContent());
$node = new SpanNode($parser->getEnvironment(), $col->getContent());
}

$col->setNode($node);
Expand Down
3 changes: 2 additions & 1 deletion src/Guides/RestructuredText/Directives/Replace.php
Expand Up @@ -5,6 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\RestructuredText\Parser;

/**
Expand All @@ -28,6 +29,6 @@ public function processNode(
string $data,
array $options
) : ?Node {
return $parser->createSpanNode($data);
return new SpanNode($parser->getEnvironment(), $data);
}
}
9 changes: 0 additions & 9 deletions src/Guides/RestructuredText/Parser.php
Expand Up @@ -8,7 +8,6 @@
use phpDocumentor\Guides\Configuration;
use phpDocumentor\Guides\Environment;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\Parser as ParserInterface;
use phpDocumentor\Guides\References\Doc;
use phpDocumentor\Guides\References\Reference;
Expand Down Expand Up @@ -143,14 +142,6 @@ public function getFilename() : string
return $this->filename ?: '(unknown)';
}

/**
* @param string|string[]|SpanNode $span
*/
public function createSpanNode($span) : SpanNode
{
return new SpanNode($this->environment, $span);
}

public function parse(string $contents) : DocumentNode
{
$this->getEnvironment()->reset();
Expand Down
9 changes: 5 additions & 4 deletions src/Guides/RestructuredText/Parser/DocumentParser.php
Expand Up @@ -19,6 +19,7 @@
use phpDocumentor\Guides\Nodes\SectionBeginNode;
use phpDocumentor\Guides\Nodes\SectionEndNode;
use phpDocumentor\Guides\Nodes\SeparatorNode;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\Nodes\TableNode;
use phpDocumentor\Guides\Nodes\TitleNode;
use phpDocumentor\Guides\RestructuredText\Directives\Directive;
Expand Down Expand Up @@ -412,7 +413,7 @@ private function flush() : void
$token = $this->environment->createTitle($level);

$node = new TitleNode(
$this->parser->createSpanNode($data),
new SpanNode($this->environment, $data),
$level,
$token
);
Expand Down Expand Up @@ -495,7 +496,7 @@ private function flush() : void

$buffer = $this->buffer->getLinesString();

$node = new ParagraphNode($this->parser->createSpanNode($buffer));
$node = new ParagraphNode(new SpanNode($this->environment, $buffer));

break;
}
Expand Down Expand Up @@ -653,7 +654,7 @@ private function parseListLine(?string $line, bool $flush = false) : bool

if ($listLine !== null) {
if ($this->listLine instanceof ListLine) {
$this->listLine->setText($this->parser->createSpanNode($this->listLine->getText()));
$this->listLine->setText(new SpanNode($this->environment, $this->listLine->getText()));

/** @var ListNode $listNode */
$listNode = $this->nodeBuffer;
Expand All @@ -677,7 +678,7 @@ private function parseListLine(?string $line, bool $flush = false) : bool

if ($flush) {
if ($this->listLine instanceof ListLine) {
$this->listLine->setText($this->parser->createSpanNode($this->listLine->getText()));
$this->listLine->setText(new SpanNode($this->environment, $this->listLine->getText()));

/** @var ListNode $listNode */
$listNode = $this->nodeBuffer;
Expand Down
15 changes: 11 additions & 4 deletions src/Guides/RestructuredText/Parser/LineDataParser.php
Expand Up @@ -5,6 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\Parser;

use Doctrine\Common\EventManager;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\RestructuredText\Event\OnLinkParsedEvent;
use phpDocumentor\Guides\RestructuredText\Parser;
use function array_map;
Expand Down Expand Up @@ -172,21 +173,24 @@ public function parseDefinitionList(array $lines) : DefinitionList

$classifiers = array_map(
function (string $classifier) {
return $this->parser->createSpanNode($classifier);
return new SpanNode($this->parser->getEnvironment(), $classifier);
},
array_map('trim', $parts)
);

$definitionListTerm = [
'term' => $this->parser->createSpanNode($term),
'term' => new SpanNode($this->parser->getEnvironment(), $term),
'classifiers' => $classifiers,
'definitions' => [],
];

// last line
} elseif ($definitionListTerm !== null && trim($line) === '' && count($lines) - 1 === $key) {
if ($currentDefinition !== null) {
$definitionListTerm['definitions'][] = $this->parser->createSpanNode($currentDefinition);
$definitionListTerm['definitions'][] = new SpanNode(
$this->parser->getEnvironment(),
$currentDefinition
);

$currentDefinition = null;
}
Expand All @@ -199,7 +203,10 @@ function (string $classifier) {

// empty line, start of a new definition for the current term
} elseif ($currentDefinition !== null && $definitionListTerm !== null && trim($line) === '') {
$definitionListTerm['definitions'][] = $this->parser->createSpanNode($currentDefinition);
$definitionListTerm['definitions'][] = new SpanNode(
$this->parser->getEnvironment(),
$currentDefinition
);

$currentDefinition = null;
}
Expand Down

0 comments on commit 09a7d5e

Please sign in to comment.