| Conditions | 2 |
| Paths | 2 |
| Total Lines | 85 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 83 | public function __construct( |
||
| 84 | StreamInterface $stream, |
||
| 85 | LoggerInterface $logger, |
||
| 86 | HandlerStackInterface $handlerStack, |
||
| 87 | int $libxml, |
||
| 88 | bool $handleHtmlEntitiesInXml |
||
| 89 | ) { |
||
| 90 | // @phpcs:enable |
||
| 91 | |||
| 92 | // Logger |
||
| 93 | $this->logger = $logger; |
||
| 94 | |||
| 95 | // Middleware |
||
| 96 | $this->middleware = $handlerStack; |
||
| 97 | |||
| 98 | // Libxml2 |
||
| 99 | $this->libxml = $libxml; |
||
| 100 | |||
| 101 | // Raw stream |
||
| 102 | $this->rawDocument = $this->readStream($stream); |
||
| 103 | |||
| 104 | // DOMDocument |
||
| 105 | $this->domDocument = new DOMDocument('1.0', 'utf-8'); |
||
| 106 | |||
| 107 | // Don't barf errors all over the output |
||
| 108 | \libxml_use_internal_errors(true); |
||
| 109 | |||
| 110 | // DOMDocument configuration |
||
| 111 | $this->domDocument->recover = true; |
||
| 112 | $this->domDocument->formatOutput = false; |
||
| 113 | $this->domDocument->preserveWhiteSpace = false; |
||
| 114 | $this->domDocument->resolveExternals = true; |
||
| 115 | $this->domDocument->substituteEntities = true; |
||
| 116 | $this->domDocument->strictErrorChecking = false; |
||
| 117 | $this->domDocument->validateOnParse = false; |
||
| 118 | |||
| 119 | // If enabled, force-inject the contents of `entities.dtd` into the feed. |
||
| 120 | if ($handleHtmlEntitiesInXml) { |
||
| 121 | $this->getLogger()->debug('Enabled handing HTML entities in XML.'); |
||
| 122 | $this->domDocument->loadXML($this->rawDocument, $this->libxml); |
||
| 123 | |||
| 124 | // Make sure this is an XML element instead of a comment or text. |
||
| 125 | $firstElement = $this->findNextRealNode($this->domDocument->firstChild); |
||
| 126 | |||
| 127 | // <feed, <rss, etc. |
||
| 128 | $rootElementStart = \sprintf('<%s', (string) $firstElement->nodeName); |
||
| 129 | |||
| 130 | // Read the entity definition file, and force-inject it into the XML document |
||
| 131 | $this->rawDocument = \str_replace( |
||
| 132 | $rootElementStart, |
||
| 133 | \sprintf( |
||
| 134 | '%s%s', |
||
| 135 | \trim( |
||
| 136 | \file_get_contents(\dirname(SIMPLEPIE_ROOT) . '/resources/entities.dtd') |
||
| 137 | ), |
||
| 138 | $rootElementStart |
||
| 139 | ), |
||
| 140 | $this->rawDocument |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Parse the XML document with the configured libxml options |
||
| 145 | $this->domDocument->loadXML($this->rawDocument, $this->libxml); |
||
| 146 | |||
| 147 | // Register the namespace handler. |
||
| 148 | $this->ns = (new Ns($this->domDocument)) |
||
| 149 | ->setLogger($this->getLogger()); |
||
| 150 | |||
| 151 | // Look at which namespaces the registered middleware understands. |
||
| 152 | $this->middleware->registerNamespaces($this->ns); |
||
| 153 | |||
| 154 | // Instantiate a new write-to feed object. |
||
| 155 | $this->feed = (new Feed($this->getNamespaceAlias() ?? '')) |
||
| 156 | ->setLogger($this->getLogger()); |
||
| 157 | |||
| 158 | // Invoke the registered middleware. |
||
| 159 | $this->middleware->invoke( |
||
| 160 | E\FeedType::XML, |
||
| 161 | $this->getFeed()->getRoot(), |
||
| 162 | $this->getNamespaceAlias(), |
||
| 163 | $this->xpath() |
||
| 164 | ); |
||
| 165 | |||
| 166 | // Clear the libxml errors to avoid excessive memory usage |
||
| 167 | \libxml_clear_errors(); |
||
| 168 | } |
||
| 245 |