Skip to content

Commit 9408c1d

Browse files
committed
Still More Coverage
1 parent b5f4c2a commit 9408c1d

File tree

7 files changed

+80
-34
lines changed

7 files changed

+80
-34
lines changed

src/PhpWord/Reader/Word2007/Footnotes.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,19 @@ public function read(PhpWord $phpWord): void
8181
*/
8282
private function getElement(PhpWord $phpWord, $relationId)
8383
{
84+
$returnVal = null;
8485
$getMethod = "get{$this->collection}";
8586
$collection = $phpWord->$getMethod()->getItems();
8687

8788
//not found by key, looping to search by relationId
8889
foreach ($collection as $collectionElement) {
8990
if ($collectionElement->getRelationId() == $relationId) {
90-
return $collectionElement;
91+
$returnVal = $collectionElement;
92+
93+
break;
9194
}
9295
}
9396

94-
return null;
97+
return $returnVal;
9598
}
9699
}

src/PhpWord/Shared/Css.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,17 @@ public function process(): void
4141
$cssContent = str_replace(["\r", "\n"], '', $this->cssContent);
4242
preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $cssContent, $cssExtracted);
4343
// Check if there are x selectors and x rules
44-
if (count($cssExtracted[1]) != count($cssExtracted[2])) {
45-
return;
46-
}
47-
48-
foreach ($cssExtracted[1] as $key => $selector) {
49-
$rules = trim($cssExtracted[2][$key]);
50-
$rules = explode(';', $rules);
51-
foreach ($rules as $rule) {
52-
if (empty($rule)) {
53-
continue;
44+
if (count($cssExtracted[1]) === count($cssExtracted[2])) {
45+
foreach ($cssExtracted[1] as $key => $selector) {
46+
$rules = trim($cssExtracted[2][$key]);
47+
$rules = explode(';', $rules);
48+
foreach ($rules as $rule) {
49+
if (empty($rule)) {
50+
continue;
51+
}
52+
[$key, $value] = explode(':', trim($rule));
53+
$this->styles[$this->sanitize($selector)][$this->sanitize($key)] = $this->sanitize($value);
5454
}
55-
[$key, $value] = explode(':', trim($rule));
56-
$this->styles[$this->sanitize($selector)][$this->sanitize($key)] = $this->sanitize($value);
5755
}
5856
}
5957
}

src/PhpWord/Shared/Text.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818

1919
namespace PhpOffice\PhpWord\Shared;
2020

21-
use PhpOffice\PhpWord\Exception\Exception;
21+
use PhpOffice\PhpWord\Exception\Exception as WordException;
2222

23-
/**
24-
* Text.
25-
*/
2623
class Text
2724
{
2825
/**
@@ -37,6 +34,9 @@ class Text
3734
*/
3835
private static function buildControlCharacters(): void
3936
{
37+
if (!empty(self::$controlCharacters)) {
38+
return;
39+
}
4040
for ($i = 0; $i <= 19; ++$i) {
4141
if ($i != 9 && $i != 10 && $i != 13) {
4242
$find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
@@ -63,9 +63,7 @@ private static function buildControlCharacters(): void
6363
*/
6464
public static function controlCharacterPHP2OOXML($value = '')
6565
{
66-
if (empty(self::$controlCharacters)) {
67-
self::buildControlCharacters();
68-
}
66+
self::buildControlCharacters();
6967

7068
return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
7169
}
@@ -119,9 +117,7 @@ public static function chr($dec)
119117
*/
120118
public static function controlCharacterOOXML2PHP($value = '')
121119
{
122-
if (empty(self::$controlCharacters)) {
123-
self::buildControlCharacters();
124-
}
120+
self::buildControlCharacters();
125121

126122
return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
127123
}
@@ -149,15 +145,26 @@ public static function toUTF8($value = '')
149145
{
150146
if (null !== $value && !self::isUTF8($value)) {
151147
// PHP8.2 : utf8_encode is deprecated, but mb_convert_encoding always usable
152-
$value = (function_exists('mb_convert_encoding')) ? mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1') : utf8_encode($value);
148+
$value = static::mbConvertEncoding($value);
153149
if ($value === false) {
154-
throw new Exception('Unable to convert text to UTF-8');
150+
throw new WordException('Unable to convert text to UTF-8');
155151
}
156152
}
157153

158154
return $value;
159155
}
160156

157+
/**
158+
* @param string $value
159+
*
160+
* @return false|string
161+
*/
162+
protected static function mbConvertEncoding($value)
163+
{
164+
// PHP8.2 : utf8_encode is deprecated, but mb_convert_encoding always usable
165+
return (function_exists('mb_convert_encoding')) ? mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1') : utf8_encode($value);
166+
}
167+
161168
/**
162169
* Returns unicode from UTF8 text.
163170
*

src/PhpWord/Writer/EPub3.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,12 @@ public function save(string $filename): void
7272

7373
// Add other files
7474
foreach ($this->parts as $partName => $fileName) {
75-
if ($fileName === '') {
76-
continue;
75+
if ($fileName !== '') {
76+
$part = $this->getWriterPart($partName);
77+
if ($part instanceof AbstractPart) {
78+
$zip->addFromString($fileName, $part->write());
79+
}
7780
}
78-
$part = $this->getWriterPart($partName);
79-
if (!$part instanceof AbstractPart) {
80-
continue;
81-
}
82-
$zip->addFromString($fileName, $part->write());
8381
}
8482

8583
// Close zip archive
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWordTests\Shared;
20+
21+
class Text2 extends \PhpOffice\PhpWord\Shared\Text
22+
{
23+
/**
24+
* @param string $value
25+
*
26+
* @return false|string
27+
*/
28+
protected static function mbConvertEncoding($value)
29+
{
30+
return false;
31+
}
32+
}

tests/PhpWordTests/Shared/TextTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace PhpOffice\PhpWordTests\Shared;
2020

21+
use PhpOffice\PhpWord\Exception\Exception as WordException;
2122
use PhpOffice\PhpWord\Shared\Text;
2223

2324
/**
@@ -87,4 +88,11 @@ public function testRemoveUnderscorePrefix(): void
8788
{
8889
self::assertEquals('item', Text::removeUnderscorePrefix('_item'));
8990
}
91+
92+
public function testFailToUtf8(): void
93+
{
94+
$this->expectException(WordException::class);
95+
$this->expectExceptionMessage('Unable to convert text to UTF-8');
96+
Text2::toUTF8("\x80");
97+
}
9098
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use RegexIterator;
2424
use RuntimeException;
2525

26-
class SampleTest extends TestCase
26+
class ZampleTest extends TestCase
2727
{
2828
/** @var bool */
2929
protected static $alwaysTrue = true;

0 commit comments

Comments
 (0)