Skip to content

Commit e00b551

Browse files
committed
Breakdown HTML Writer to head and body parts and more cyclomatic complexity reductions
1 parent d79ca9a commit e00b551

File tree

12 files changed

+389
-243
lines changed

12 files changed

+389
-243
lines changed

src/PhpWord/Autoloader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function autoload($class)
4646
$file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
4747
$file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
4848
if (file_exists($file)) {
49+
/** @noinspection PhpIncludeInspection Dynamic includes */
4950
require_once $file;
5051
}
5152
}

src/PhpWord/Reader/Word2007/Document.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ private function readWPNode(XMLReader $xmlReader, \DOMElement $node, Section &$s
158158
// Section properties
159159
if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) {
160160
$sectPrNode = $xmlReader->getElement('w:pPr/w:sectPr', $node);
161-
$this->readWSectPrNode($xmlReader, $sectPrNode, $section);
161+
if ($sectPrNode !== null) {
162+
$this->readWSectPrNode($xmlReader, $sectPrNode, $section);
163+
}
162164
$section = $this->phpWord->addSection();
163165
}
164166
}
@@ -172,10 +174,8 @@ private function readWPNode(XMLReader $xmlReader, \DOMElement $node, Section &$s
172174
*/
173175
private function readWSectPrNode(XMLReader $xmlReader, \DOMElement $node, Section &$section)
174176
{
175-
if ($node !== null) {
176-
$settings = $this->readSectionStyle($xmlReader, $node);
177-
$section->setSettings($settings);
178-
$this->readHeaderFooter($settings, $section);
179-
}
177+
$settings = $this->readSectionStyle($xmlReader, $node);
178+
$section->setSettings($settings);
179+
$this->readHeaderFooter($settings, $section);
180180
}
181181
}

src/PhpWord/Shared/String.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,28 @@ public static function toUTF8($value = '')
8686
}
8787

8888
/**
89-
* Returns unicode from UTF8 text
89+
* Returns unicode array from UTF8 text
90+
*
91+
* The function is splitted to reduce cyclomatic complexity
9092
*
9193
* @param string $text UTF8 text
9294
* @return string Unicode text
9395
* @since 0.11.0
94-
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
9596
*/
9697
public static function toUnicode($text)
98+
{
99+
return self::unicodeToEntities(self::utf8ToUnicode($text));
100+
}
101+
102+
/**
103+
* Returns unicode from UTF8 text
104+
*
105+
* @param string $text UTF8 text
106+
* @return array
107+
* @since 0.11.0
108+
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
109+
*/
110+
private static function utf8ToUnicode($text)
97111
{
98112
$unicode = array();
99113
$values = array();
@@ -122,8 +136,21 @@ public static function toUnicode($text)
122136
}
123137
}
124138

125-
// Converts text with utf8 characters into rtf utf8 entites preserving ascii
139+
return $unicode;
140+
}
141+
142+
/**
143+
* Returns entites from unicode array
144+
*
145+
* @param array $unicode
146+
* @return string
147+
* @since 0.11.0
148+
* @link http://www.randomchaos.com/documents/?source=php_and_unicode
149+
*/
150+
private static function unicodeToEntities($unicode)
151+
{
126152
$entities = '';
153+
127154
foreach ($unicode as $value) {
128155
if ($value != 65279) {
129156
$entities .= $value > 127 ? '\uc0{\u' . $value . '}' : chr($value);

src/PhpWord/Writer/HTML.php

Lines changed: 13 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@
1919

2020
use PhpOffice\PhpWord\Exception\Exception;
2121
use PhpOffice\PhpWord\PhpWord;
22-
use PhpOffice\PhpWord\Settings;
23-
use PhpOffice\PhpWord\Style;
24-
use PhpOffice\PhpWord\Style\Font;
25-
use PhpOffice\PhpWord\Style\Paragraph;
26-
use PhpOffice\PhpWord\Writer\HTML\Element\Container;
27-
use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
28-
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
29-
use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
30-
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
3122

3223
/**
3324
* HTML writer
@@ -57,6 +48,17 @@ class HTML extends AbstractWriter implements WriterInterface
5748
public function __construct(PhpWord $phpWord = null)
5849
{
5950
$this->setPhpWord($phpWord);
51+
52+
$this->parts = array('Head', 'Body');
53+
foreach ($this->parts as $partName) {
54+
$partClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Part\\' . $partName;
55+
if (class_exists($partClass)) {
56+
/** @var \PhpOffice\PhpWord\Writer\HTML\Part\AbstractPart $part Type hint */
57+
$part = new $partClass();
58+
$part->setParentWriter($this);
59+
$this->writerParts[strtolower($partName)] = $part;
60+
}
61+
}
6062
}
6163

6264
/**
@@ -89,164 +91,13 @@ public function writeDocument()
8991
$content .= '<!DOCTYPE html>' . PHP_EOL;
9092
$content .= '<!-- Generated by PHPWord -->' . PHP_EOL;
9193
$content .= '<html>' . PHP_EOL;
92-
$content .= '<head>' . PHP_EOL;
93-
$content .= $this->writeHead();
94-
$content .= '</head>' . PHP_EOL;
95-
$content .= '<body>' . PHP_EOL;
96-
$content .= $this->writeBody();
97-
$content .= $this->writeNotes();
98-
$content .= '</body>' . PHP_EOL;
94+
$content .= $this->getWriterPart('Head')->write();
95+
$content .= $this->getWriterPart('Body')->write();
9996
$content .= '</html>' . PHP_EOL;
10097

10198
return $content;
10299
}
103100

104-
/**
105-
* Generate HTML header
106-
*
107-
* @return string
108-
*/
109-
private function writeHead()
110-
{
111-
$phpWord = $this->getPhpWord();
112-
$properties = $phpWord->getDocumentProperties();
113-
$propertiesMapping = array(
114-
'creator' => 'author',
115-
'title' => '',
116-
'description' => '',
117-
'subject' => '',
118-
'keywords' => '',
119-
'category' => '',
120-
'company' => '',
121-
'manager' => ''
122-
);
123-
$title = $properties->getTitle();
124-
$title = ($title != '') ? $title : 'PHPWord';
125-
126-
$content = '';
127-
$content .= '<meta charset="UTF-8" />' . PHP_EOL;
128-
$content .= '<title>' . htmlspecialchars($title) . '</title>' . PHP_EOL;
129-
foreach ($propertiesMapping as $key => $value) {
130-
$value = ($value == '') ? $key : $value;
131-
$method = "get" . $key;
132-
if ($properties->$method() != '') {
133-
$content .= '<meta name="' . $value . '" content="' .
134-
htmlspecialchars($properties->$method()) . '" />' . PHP_EOL;
135-
}
136-
}
137-
$content .= $this->writeStyles();
138-
139-
return $content;
140-
}
141-
142-
/**
143-
* Get content
144-
*
145-
* @return string
146-
*/
147-
private function writeBody()
148-
{
149-
$phpWord = $this->getPhpWord();
150-
$content = '';
151-
152-
$sections = $phpWord->getSections();
153-
$countSections = count($sections);
154-
155-
if ($countSections > 0) {
156-
foreach ($sections as $section) {
157-
$writer = new Container($this, $section);
158-
$content .= $writer->write();
159-
}
160-
}
161-
162-
return $content;
163-
}
164-
165-
/**
166-
* Get styles
167-
*
168-
* @return string
169-
*/
170-
private function writeStyles()
171-
{
172-
$css = '<style>' . PHP_EOL;
173-
174-
// Default styles
175-
$defaultStyles = array(
176-
'*' => array(
177-
'font-family' => Settings::getDefaultFontName(),
178-
'font-size' => Settings::getDefaultFontSize() . 'pt',
179-
),
180-
'a.NoteRef' => array(
181-
'text-decoration' => 'none',
182-
),
183-
'hr' => array(
184-
'height' => '1px',
185-
'padding' => '0',
186-
'margin' => '1em 0',
187-
'border' => '0',
188-
'border-top' => '1px solid #CCC',
189-
),
190-
);
191-
foreach ($defaultStyles as $selector => $style) {
192-
$styleWriter = new GenericStyleWriter($style);
193-
$css .= $selector . ' {' . $styleWriter->write() . '}' . PHP_EOL;
194-
}
195-
196-
// Custom styles
197-
$customStyles = Style::getStyles();
198-
if (is_array($customStyles)) {
199-
foreach ($customStyles as $name => $style) {
200-
if ($style instanceof Font) {
201-
$styleWriter = new FontStyleWriter($style);
202-
if ($style->getStyleType() == 'title') {
203-
$name = str_replace('Heading_', 'h', $name);
204-
} else {
205-
$name = '.' . $name;
206-
}
207-
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
208-
} elseif ($style instanceof Paragraph) {
209-
$styleWriter = new ParagraphStyleWriter($style);
210-
$name = '.' . $name;
211-
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
212-
}
213-
}
214-
}
215-
$css .= '</style>' . PHP_EOL;
216-
217-
return $css;
218-
}
219-
220-
/**
221-
* Write footnote/endnote contents as textruns
222-
*/
223-
private function writeNotes()
224-
{
225-
$phpWord = $this->getPhpWord();
226-
$content = PHP_EOL;
227-
228-
if (!empty($this->notes)) {
229-
$content .= "<hr />" . PHP_EOL;
230-
foreach ($this->notes as $noteId => $noteMark) {
231-
list($noteType, $noteTypeId) = explode('-', $noteMark);
232-
$method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
233-
$collection = $phpWord->$method()->getItems();
234-
235-
if (array_key_exists($noteTypeId, $collection)) {
236-
$element = $collection[$noteTypeId];
237-
$noteAnchor = "<a name=\"note-{$noteId}\" />";
238-
$noteAnchor .= "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
239-
240-
$writer = new TextRunWriter($this, $element);
241-
$writer->setOpeningText($noteAnchor);
242-
$content .= $writer->write();
243-
}
244-
}
245-
}
246-
247-
return $content;
248-
}
249-
250101
/**
251102
* Get is PDF
252103
*

src/PhpWord/Writer/HTML/Element/Image.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private function getBase64ImageData(ImageElement $element)
8888
} else {
8989
$actualSource = $source;
9090
}
91-
if (is_null($actualSource)) {
91+
if ($actualSource === null) {
9292
return null;
9393
}
9494

@@ -100,12 +100,13 @@ private function getBase64ImageData(ImageElement $element)
100100
$imageBinary = ob_get_contents();
101101
ob_end_clean();
102102
} else {
103-
if ($fileHandle = fopen($actualSource, 'rb', false)) {
103+
$fileHandle = fopen($actualSource, 'rb', false);
104+
if ($fileHandle !== false) {
104105
$imageBinary = fread($fileHandle, filesize($actualSource));
105106
fclose($fileHandle);
106107
}
107108
}
108-
if (!is_null($imageBinary)) {
109+
if ($imageBinary !== null) {
109110
$base64 = chunk_split(base64_encode($imageBinary));
110111
$imageData = 'data:' . $imageType . ';base64,' . $base64;
111112
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\HTML\Part;
19+
20+
use PhpOffice\PhpWord\Exception\Exception;
21+
use PhpOffice\PhpWord\Writer\AbstractWriter;
22+
23+
/**
24+
* Abstract HTML part writer
25+
*
26+
* @since 0.11.0
27+
*/
28+
abstract class AbstractPart
29+
{
30+
/**
31+
* Parent writer
32+
*
33+
* @var \PhpOffice\PhpWord\Writer\AbstractWriter
34+
*/
35+
private $parentWriter;
36+
37+
/**
38+
* Write part
39+
*
40+
* @return string
41+
*/
42+
abstract public function write();
43+
44+
/**
45+
* Set parent writer
46+
*
47+
* @param \PhpOffice\PhpWord\Writer\AbstractWriter $writer
48+
*/
49+
public function setParentWriter(AbstractWriter $writer = null)
50+
{
51+
$this->parentWriter = $writer;
52+
}
53+
54+
/**
55+
* Get parent writer
56+
*
57+
* @return \PhpOffice\PhpWord\Writer\AbstractWriter
58+
* @throws \PhpOffice\PhpWord\Exception\Exception
59+
*/
60+
public function getParentWriter()
61+
{
62+
if ($this->parentWriter !== null) {
63+
return $this->parentWriter;
64+
} else {
65+
throw new Exception('No parent WriterInterface assigned.');
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)