diff --git a/packages/FileFormatter/Formatter/XmlFileFormatter.php b/packages/FileFormatter/Formatter/XmlFileFormatter.php index 2b391755fab..fa0ea229890 100644 --- a/packages/FileFormatter/Formatter/XmlFileFormatter.php +++ b/packages/FileFormatter/Formatter/XmlFileFormatter.php @@ -16,6 +16,24 @@ */ final class XmlFileFormatter implements FileFormatterInterface { + /** + * @see https://regex101.com/r/uTmMcr/1 + * @var string + */ + private const XML_PARTS_REGEX = '#(>)(<)(\/*)#'; + + /** + * @see https://regex101.com/r/hSG1JT/1 + * @var string + */ + private const IS_OPENING_TAG_REGEX = '#^<[^\/]*>$#'; + + /** + * @see https://regex101.com/r/ywS62K/1 + * @var string + */ + private const IS_CLOSING_TAG_REGEX = '#^\s*<\/#'; + private ?int $depth = null; private int $indent = 4; @@ -75,9 +93,7 @@ private function formatXml(string $xml): string */ private function getXmlParts(string $xml): array { - $xmlParts = '#(>)(<)(\/*)#'; - - $withNewLines = Strings::replace(trim($xml), $xmlParts, "$1\n$2$3"); + $withNewLines = Strings::replace(trim($xml), self::XML_PARTS_REGEX, "$1\n$2$3"); return explode("\n", $withNewLines); } @@ -125,16 +141,12 @@ private function getPaddedString(string $part): string private function isOpeningTag(string $part): bool { - $isOpeningTag = '#^<[^\/]*>$#'; - - return (bool) Strings::match($part, $isOpeningTag); + return (bool) Strings::match($part, self::IS_OPENING_TAG_REGEX); } private function isClosingTag(string $part): bool { - $isClosingTag = '#^\s*<\/#'; - - return (bool) Strings::match($part, $isClosingTag); + return (bool) Strings::match($part, self::IS_CLOSING_TAG_REGEX); } private function isOpeningCdataTag(string $part): bool