Skip to content

Commit

Permalink
Reduce duplicated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 25, 2019
1 parent eeebb28 commit a3cef6e
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/Exporter/Gv.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,10 @@ public function export(NodeInterface $node): string
}

if (\is_array($attribute)) {
$attributesText = \array_map(
static function ($key, $value) {
return \sprintf('%s="%s"', $key, $value);
},
\array_keys($attribute),
$attribute
);

$attributes .= \sprintf(
' %s %s' . "\n",
$key,
'[' . \implode(' ', $attributesText) . ']'
$this->attributesArrayToText($attribute)
);

continue;
Expand Down Expand Up @@ -131,6 +123,28 @@ public function setGraphAttributes(array $attributes): Gv
return $this;
}

/**
* Converts an attributes array to string.
*
* @param array $attributes
* The attributes.
*
* @return string
* The attributes as string.
*/
protected function attributesArrayToText(array $attributes): string
{
$attributesText = \array_map(
static function ($key, $value) {
return \sprintf('%s="%s"', $key, $value);
},
\array_keys($attributes),
$attributes
);

return '[' . \implode(' ', $attributesText) . ']';
}

/**
* Recursively find all the edges in a tree.
*
Expand Down Expand Up @@ -213,14 +227,6 @@ protected function getNodeAttributes(NodeInterface $node): string
}
}

$attributesText = \array_map(
static function ($key, $value) {
return \sprintf('%s="%s"', $key, $value);
},
\array_keys($attributes),
$attributes
);

return '[' . \implode(' ', $attributesText) . ']';
return $this->attributesArrayToText($attributes);
}
}

0 comments on commit a3cef6e

Please sign in to comment.