Phpstan Baseline Fixes 2022-09-21 (#3080)

* Phpstan Baseline Fixes 2022-09-21

Eliminate about 200 more lines from Phpstan baseline. For Helper/Sample and Helper/Html and others, many properties are declared as protected despite the fact that the classes do not extend any other class, and there are no classes which extend them. They are changed to private; this could be a breaking change in circumstances for which I cannot think of a use case (user extends class for some reason).

* Slightly Botched Merge Commit

Hope this fixes phpstan.
This commit is contained in:
oleibman 2022-09-30 09:00:13 -07:00 committed by GitHub
parent b8201a79c5
commit 35b42cc180
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 200 additions and 300 deletions

View File

@ -1110,156 +1110,6 @@ parameters:
count: 2 count: 2
path: src/PhpSpreadsheet/DefinedName.php path: src/PhpSpreadsheet/DefinedName.php
-
message: "#^Cannot call method setBold\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Cannot call method setColor\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Cannot call method setItalic\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Cannot call method setName\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Cannot call method setSize\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Cannot call method setStrikethrough\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Cannot call method setSubscript\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Cannot call method setSuperscript\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Cannot call method setUnderline\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:startFontTag\\(\\) has parameter \\$tag with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$bold has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$color has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$colourMap has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$endTagCallbacks has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$face has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$italic has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$size has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$stack has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$startTagCallbacks has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$strikethrough has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$stringData has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$subscript has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$superscript has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Html\\:\\:\\$underline has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Html.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Sample\\:\\:getSamples\\(\\) should return array\\<array\\<string\\>\\> but returns array\\<string, array\\<string, array\\|string\\>\\>\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Sample.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Helper\\\\Sample\\:\\:log\\(\\) has parameter \\$message with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Sample.php
-
message: "#^Parameter \\#1 \\$directory of class RecursiveDirectoryIterator constructor expects string, string\\|false given\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Sample.php
-
message: "#^Parameter \\#1 \\$filename of function unlink expects string, string\\|false given\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Sample.php
-
message: "#^Parameter \\#1 \\$path of function pathinfo expects string, array\\|string given\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Sample.php
-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, string\\|false given\\.$#"
count: 1
path: src/PhpSpreadsheet/Helper/Sample.php
- -
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\IOFactory\\:\\:createReader\\(\\) should return PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\IReader but returns object\\.$#" message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\IOFactory\\:\\:createReader\\(\\) should return PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\IReader but returns object\\.$#"
count: 1 count: 1
@ -1620,41 +1470,6 @@ parameters:
count: 8 count: 8
path: src/PhpSpreadsheet/Reader/Xls.php path: src/PhpSpreadsheet/Reader/Xls.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\Color\\\\BIFF5\\:\\:\\$map has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls/Color/BIFF5.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\Color\\\\BIFF8\\:\\:\\$map has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls/Color/BIFF8.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\Color\\\\BuiltIn\\:\\:\\$map has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls/Color/BuiltIn.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\ErrorCode\\:\\:\\$map has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls/ErrorCode.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\RC4\\:\\:\\$i has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls/RC4.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\RC4\\:\\:\\$j has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls/RC4.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xls\\\\RC4\\:\\:\\$s has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Xls/RC4.php
- -
message: "#^Cannot access property \\$r on SimpleXMLElement\\|null\\.$#" message: "#^Cannot access property \\$r on SimpleXMLElement\\|null\\.$#"
count: 2 count: 2
@ -2145,11 +1960,6 @@ parameters:
count: 1 count: 1
path: src/PhpSpreadsheet/Shared/OLERead.php path: src/PhpSpreadsheet/Shared/OLERead.php
-
message: "#^Static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\TimeZone\\:\\:validateTimeZone\\(\\) is unused\\.$#"
count: 1
path: src/PhpSpreadsheet/Shared/TimeZone.php
- -
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Trend\\\\BestFit\\:\\:calculateGoodnessOfFit\\(\\) has parameter \\$const with no type specified\\.$#" message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Trend\\\\BestFit\\:\\:calculateGoodnessOfFit\\(\\) has parameter \\$const with no type specified\\.$#"
count: 1 count: 1

View File

@ -32,5 +32,5 @@ $worksheet->getStyle('B8:C8')->getNumberFormat()->setFormatCode('$#,##0.00');
$helper->log($worksheet->getCell('B8')->getValue()); $helper->log($worksheet->getCell('B8')->getValue());
$helper->log('FV() Result is ' . $worksheet->getCell('B8')->getFormattedValue()); $helper->log('FV() Result is ' . $worksheet->getCell('B8')->getFormattedValue());
$helper->log($worksheet->getCell('C6')->getValue()); $helper->log($worksheet->getCell('C8')->getValue());
$helper->log('FV() Result is ' . $worksheet->getCell('C8')->getFormattedValue()); $helper->log('FV() Result is ' . $worksheet->getCell('C8')->getFormattedValue());

View File

@ -12,7 +12,7 @@ use PhpOffice\PhpSpreadsheet\Style\Font;
class Html class Html
{ {
protected static $colourMap = [ private const COLOUR_MAP = [
'aliceblue' => 'f0f8ff', 'aliceblue' => 'f0f8ff',
'antiquewhite' => 'faebd7', 'antiquewhite' => 'faebd7',
'antiquewhite1' => 'ffefdb', 'antiquewhite1' => 'ffefdb',
@ -532,25 +532,34 @@ class Html
'yellowgreen' => '9acd32', 'yellowgreen' => '9acd32',
]; ];
protected $face; /** @var ?string */
private $face;
protected $size; /** @var ?string */
private $size;
protected $color; /** @var ?string */
private $color;
protected $bold = false; /** @var bool */
private $bold = false;
protected $italic = false; /** @var bool */
private $italic = false;
protected $underline = false; /** @var bool */
private $underline = false;
protected $superscript = false; /** @var bool */
private $superscript = false;
protected $subscript = false; /** @var bool */
private $subscript = false;
protected $strikethrough = false; /** @var bool */
private $strikethrough = false;
protected $startTagCallbacks = [ private const START_TAG_CALLBACKS = [
'font' => 'startFontTag', 'font' => 'startFontTag',
'b' => 'startBoldTag', 'b' => 'startBoldTag',
'strong' => 'startBoldTag', 'strong' => 'startBoldTag',
@ -563,7 +572,7 @@ class Html
'sub' => 'startSubscriptTag', 'sub' => 'startSubscriptTag',
]; ];
protected $endTagCallbacks = [ private const END_TAG_CALLBACKS = [
'font' => 'endFontTag', 'font' => 'endFontTag',
'b' => 'endBoldTag', 'b' => 'endBoldTag',
'strong' => 'endBoldTag', 'strong' => 'endBoldTag',
@ -584,16 +593,18 @@ class Html
'h6' => 'breakTag', 'h6' => 'breakTag',
]; ];
protected $stack = []; /** @var array */
private $stack = [];
protected $stringData = ''; /** @var string */
private $stringData = '';
/** /**
* @var RichText * @var RichText
*/ */
protected $richTextObject; private $richTextObject;
protected function initialise(): void private function initialise(): void
{ {
$this->face = $this->size = $this->color = null; $this->face = $this->size = $this->color = null;
$this->bold = $this->italic = $this->underline = $this->superscript = $this->subscript = $this->strikethrough = false; $this->bold = $this->italic = $this->underline = $this->superscript = $this->subscript = $this->strikethrough = false;
@ -633,7 +644,7 @@ class Html
return $this->richTextObject; return $this->richTextObject;
} }
protected function cleanWhitespace(): void private function cleanWhitespace(): void
{ {
foreach ($this->richTextObject->getRichTextElements() as $key => $element) { foreach ($this->richTextObject->getRichTextElements() as $key => $element) {
$text = $element->getText(); $text = $element->getText();
@ -647,7 +658,7 @@ class Html
} }
} }
protected function buildTextRun(): void private function buildTextRun(): void
{ {
$text = $this->stringData; $text = $this->stringData;
if (trim($text) === '') { if (trim($text) === '') {
@ -655,37 +666,40 @@ class Html
} }
$richtextRun = $this->richTextObject->createTextRun($this->stringData); $richtextRun = $this->richTextObject->createTextRun($this->stringData);
if ($this->face) { $font = $richtextRun->getFont();
$richtextRun->getFont()->setName($this->face); if ($font !== null) {
} if ($this->face) {
if ($this->size) { $font->setName($this->face);
$richtextRun->getFont()->setSize($this->size); }
} if ($this->size) {
if ($this->color) { $font->setSize($this->size);
$richtextRun->getFont()->setColor(new Color('ff' . $this->color)); }
} if ($this->color) {
if ($this->bold) { $font->setColor(new Color('ff' . $this->color));
$richtextRun->getFont()->setBold(true); }
} if ($this->bold) {
if ($this->italic) { $font->setBold(true);
$richtextRun->getFont()->setItalic(true); }
} if ($this->italic) {
if ($this->underline) { $font->setItalic(true);
$richtextRun->getFont()->setUnderline(Font::UNDERLINE_SINGLE); }
} if ($this->underline) {
if ($this->superscript) { $font->setUnderline(Font::UNDERLINE_SINGLE);
$richtextRun->getFont()->setSuperscript(true); }
} if ($this->superscript) {
if ($this->subscript) { $font->setSuperscript(true);
$richtextRun->getFont()->setSubscript(true); }
} if ($this->subscript) {
if ($this->strikethrough) { $font->setSubscript(true);
$richtextRun->getFont()->setStrikethrough(true); }
if ($this->strikethrough) {
$font->setStrikethrough(true);
}
} }
$this->stringData = ''; $this->stringData = '';
} }
protected function rgbToColour(string $rgbValue): string private function rgbToColour(string $rgbValue): string
{ {
preg_match_all('/\d+/', $rgbValue, $values); preg_match_all('/\d+/', $rgbValue, $values);
foreach ($values[0] as &$value) { foreach ($values[0] as &$value) {
@ -697,100 +711,103 @@ class Html
public static function colourNameLookup(string $colorName): string public static function colourNameLookup(string $colorName): string
{ {
return self::$colourMap[$colorName] ?? ''; return self::COLOUR_MAP[$colorName] ?? '';
} }
protected function startFontTag($tag): void private function startFontTag(DOMElement $tag): void
{ {
foreach ($tag->attributes as $attribute) { $attrs = $tag->attributes;
$attributeName = strtolower($attribute->name); if ($attrs !== null) {
$attributeValue = $attribute->value; foreach ($attrs as $attribute) {
$attributeName = strtolower($attribute->name);
$attributeValue = $attribute->value;
if ($attributeName == 'color') { if ($attributeName == 'color') {
if (preg_match('/rgb\s*\(/', $attributeValue)) { if (preg_match('/rgb\s*\(/', $attributeValue)) {
$this->$attributeName = $this->rgbToColour($attributeValue); $this->$attributeName = $this->rgbToColour($attributeValue);
} elseif (strpos(trim($attributeValue), '#') === 0) { } elseif (strpos(trim($attributeValue), '#') === 0) {
$this->$attributeName = ltrim($attributeValue, '#'); $this->$attributeName = ltrim($attributeValue, '#');
} else {
$this->$attributeName = static::colourNameLookup($attributeValue);
}
} else { } else {
$this->$attributeName = static::colourNameLookup($attributeValue); $this->$attributeName = $attributeValue;
} }
} else {
$this->$attributeName = $attributeValue;
} }
} }
} }
protected function endFontTag(): void private function endFontTag(): void
{ {
$this->face = $this->size = $this->color = null; $this->face = $this->size = $this->color = null;
} }
protected function startBoldTag(): void private function startBoldTag(): void
{ {
$this->bold = true; $this->bold = true;
} }
protected function endBoldTag(): void private function endBoldTag(): void
{ {
$this->bold = false; $this->bold = false;
} }
protected function startItalicTag(): void private function startItalicTag(): void
{ {
$this->italic = true; $this->italic = true;
} }
protected function endItalicTag(): void private function endItalicTag(): void
{ {
$this->italic = false; $this->italic = false;
} }
protected function startUnderlineTag(): void private function startUnderlineTag(): void
{ {
$this->underline = true; $this->underline = true;
} }
protected function endUnderlineTag(): void private function endUnderlineTag(): void
{ {
$this->underline = false; $this->underline = false;
} }
protected function startSubscriptTag(): void private function startSubscriptTag(): void
{ {
$this->subscript = true; $this->subscript = true;
} }
protected function endSubscriptTag(): void private function endSubscriptTag(): void
{ {
$this->subscript = false; $this->subscript = false;
} }
protected function startSuperscriptTag(): void private function startSuperscriptTag(): void
{ {
$this->superscript = true; $this->superscript = true;
} }
protected function endSuperscriptTag(): void private function endSuperscriptTag(): void
{ {
$this->superscript = false; $this->superscript = false;
} }
protected function startStrikethruTag(): void private function startStrikethruTag(): void
{ {
$this->strikethrough = true; $this->strikethrough = true;
} }
protected function endStrikethruTag(): void private function endStrikethruTag(): void
{ {
$this->strikethrough = false; $this->strikethrough = false;
} }
protected function breakTag(): void private function breakTag(): void
{ {
$this->stringData .= "\n"; $this->stringData .= "\n";
} }
protected function parseTextNode(DOMText $textNode): void private function parseTextNode(DOMText $textNode): void
{ {
$domText = (string) preg_replace( $domText = (string) preg_replace(
'/\s+/u', '/\s+/u',
@ -804,7 +821,7 @@ class Html
/** /**
* @param string $callbackTag * @param string $callbackTag
*/ */
protected function handleCallback(DOMElement $element, $callbackTag, array $callbacks): void private function handleCallback(DOMElement $element, $callbackTag, array $callbacks): void
{ {
if (isset($callbacks[$callbackTag])) { if (isset($callbacks[$callbackTag])) {
$elementHandler = $callbacks[$callbackTag]; $elementHandler = $callbacks[$callbackTag];
@ -815,20 +832,20 @@ class Html
} }
} }
protected function parseElementNode(DOMElement $element): void private function parseElementNode(DOMElement $element): void
{ {
$callbackTag = strtolower($element->nodeName); $callbackTag = strtolower($element->nodeName);
$this->stack[] = $callbackTag; $this->stack[] = $callbackTag;
$this->handleCallback($element, $callbackTag, $this->startTagCallbacks); $this->handleCallback($element, $callbackTag, self::START_TAG_CALLBACKS);
$this->parseElements($element); $this->parseElements($element);
array_pop($this->stack); array_pop($this->stack);
$this->handleCallback($element, $callbackTag, $this->endTagCallbacks); $this->handleCallback($element, $callbackTag, self::END_TAG_CALLBACKS);
} }
protected function parseElements(DOMNode $element): void private function parseElements(DOMNode $element): void
{ {
foreach ($element->childNodes as $child) { foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) { if ($child instanceof DOMText) {

View File

@ -77,6 +77,11 @@ class Sample
{ {
// Populate samples // Populate samples
$baseDir = realpath(__DIR__ . '/../../../samples'); $baseDir = realpath(__DIR__ . '/../../../samples');
if ($baseDir === false) {
// @codeCoverageIgnoreStart
throw new RuntimeException('realpath returned false');
// @codeCoverageIgnoreEnd
}
$directory = new RecursiveDirectoryIterator($baseDir); $directory = new RecursiveDirectoryIterator($baseDir);
$iterator = new RecursiveIteratorIterator($directory); $iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH); $regex = new RegexIterator($iterator, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH);
@ -84,6 +89,11 @@ class Sample
$files = []; $files = [];
foreach ($regex as $file) { foreach ($regex as $file) {
$file = str_replace(str_replace('\\', '/', $baseDir) . '/', '', str_replace('\\', '/', $file[0])); $file = str_replace(str_replace('\\', '/', $baseDir) . '/', '', str_replace('\\', '/', $file[0]));
if (is_array($file)) {
// @codeCoverageIgnoreStart
throw new RuntimeException('str_replace returned array');
// @codeCoverageIgnoreEnd
}
$info = pathinfo($file); $info = pathinfo($file);
$category = str_replace('_', ' ', $info['dirname'] ?? ''); $category = str_replace('_', ' ', $info['dirname'] ?? '');
$name = str_replace('_', ' ', (string) preg_replace('/(|\.php)/', '', $info['filename'])); $name = str_replace('_', ' ', (string) preg_replace('/(|\.php)/', '', $info['filename']));
@ -172,12 +182,17 @@ class Sample
public function getTemporaryFilename($extension = 'xlsx') public function getTemporaryFilename($extension = 'xlsx')
{ {
$temporaryFilename = tempnam($this->getTemporaryFolder(), 'phpspreadsheet-'); $temporaryFilename = tempnam($this->getTemporaryFolder(), 'phpspreadsheet-');
if ($temporaryFilename === false) {
// @codeCoverageIgnoreStart
throw new RuntimeException('tempnam returned false');
// @codeCoverageIgnoreEnd
}
unlink($temporaryFilename); unlink($temporaryFilename);
return $temporaryFilename . '.' . $extension; return $temporaryFilename . '.' . $extension;
} }
public function log($message): void public function log(string $message): void
{ {
$eol = $this->isCli() ? PHP_EOL : '<br />'; $eol = $this->isCli() ? PHP_EOL : '<br />';
echo date('H:i:s ') . $message . $eol; echo date('H:i:s ') . $message . $eol;

View File

@ -4,7 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Reader\Xls\Color;
class BIFF5 class BIFF5
{ {
protected static $map = [ private const BIFF5_COLOR_MAP = [
0x08 => '000000', 0x08 => '000000',
0x09 => 'FFFFFF', 0x09 => 'FFFFFF',
0x0A => 'FF0000', 0x0A => 'FF0000',
@ -72,10 +72,6 @@ class BIFF5
*/ */
public static function lookup($color) public static function lookup($color)
{ {
if (isset(self::$map[$color])) { return ['rgb' => self::BIFF5_COLOR_MAP[$color] ?? '000000'];
return ['rgb' => self::$map[$color]];
}
return ['rgb' => '000000'];
} }
} }

View File

@ -4,7 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Reader\Xls\Color;
class BIFF8 class BIFF8
{ {
protected static $map = [ private const BIFF8_COLOR_MAP = [
0x08 => '000000', 0x08 => '000000',
0x09 => 'FFFFFF', 0x09 => 'FFFFFF',
0x0A => 'FF0000', 0x0A => 'FF0000',
@ -72,10 +72,6 @@ class BIFF8
*/ */
public static function lookup($color) public static function lookup($color)
{ {
if (isset(self::$map[$color])) { return ['rgb' => self::BIFF8_COLOR_MAP[$color] ?? '000000'];
return ['rgb' => self::$map[$color]];
}
return ['rgb' => '000000'];
} }
} }

View File

@ -4,7 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Reader\Xls\Color;
class BuiltIn class BuiltIn
{ {
protected static $map = [ private const BUILTIN_COLOR_MAP = [
0x00 => '000000', 0x00 => '000000',
0x01 => 'FFFFFF', 0x01 => 'FFFFFF',
0x02 => 'FF0000', 0x02 => 'FF0000',
@ -26,10 +26,6 @@ class BuiltIn
*/ */
public static function lookup($color) public static function lookup($color)
{ {
if (isset(self::$map[$color])) { return ['rgb' => self::BUILTIN_COLOR_MAP[$color] ?? '000000'];
return ['rgb' => self::$map[$color]];
}
return ['rgb' => '000000'];
} }
} }

View File

@ -4,7 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Reader\Xls;
class ErrorCode class ErrorCode
{ {
protected static $map = [ private const ERROR_CODE_MAP = [
0x00 => '#NULL!', 0x00 => '#NULL!',
0x07 => '#DIV/0!', 0x07 => '#DIV/0!',
0x0F => '#VALUE!', 0x0F => '#VALUE!',
@ -23,10 +23,6 @@ class ErrorCode
*/ */
public static function lookup($code) public static function lookup($code)
{ {
if (isset(self::$map[$code])) { return self::ERROR_CODE_MAP[$code] ?? false;
return self::$map[$code];
}
return false;
} }
} }

View File

@ -4,11 +4,13 @@ namespace PhpOffice\PhpSpreadsheet\Reader\Xls;
class RC4 class RC4
{ {
// Context /** @var int[] */
protected $s = []; protected $s = []; // Context
/** @var int */
protected $i = 0; protected $i = 0;
/** @var int */
protected $j = 0; protected $j = 0;
/** /**

View File

@ -35,7 +35,7 @@ class TimeZone
*/ */
public static function setTimeZone(string $timezoneName): bool public static function setTimeZone(string $timezoneName): bool
{ {
if (self::validateTimezone($timezoneName)) { if (self::validateTimeZone($timezoneName)) {
self::$timezone = $timezoneName; self::$timezone = $timezoneName;
return true; return true;
@ -67,7 +67,7 @@ class TimeZone
{ {
$timezoneName = $timezoneName ?? self::$timezone; $timezoneName = $timezoneName ?? self::$timezone;
$dtobj = Date::dateTimeFromTimestamp("$timestamp"); $dtobj = Date::dateTimeFromTimestamp("$timestamp");
if (!self::validateTimezone($timezoneName)) { if (!self::validateTimeZone($timezoneName)) {
throw new PhpSpreadsheetException("Invalid timezone $timezoneName"); throw new PhpSpreadsheetException("Invalid timezone $timezoneName");
} }
$dtobj->setTimeZone(new DateTimeZone($timezoneName)); $dtobj->setTimeZone(new DateTimeZone($timezoneName));

View File

@ -80,6 +80,7 @@ class HtmlTest extends TestCase
<tr> <tr>
<td style="background-color: #0000FF;color: #FFFFFF">Blue background</td> <td style="background-color: #0000FF;color: #FFFFFF">Blue background</td>
<td style="background-color: unknown1;color: unknown2">Unknown fore/background</td> <td style="background-color: unknown1;color: unknown2">Unknown fore/background</td>
<td style="background-color: antiquewhite2;color: aliceblue">Unknown fore/background</td>
</tr> </tr>
</table>'; </table>';
$filename = HtmlHelper::createHtml($html); $filename = HtmlHelper::createHtml($html);
@ -93,6 +94,10 @@ class HtmlTest extends TestCase
self::assertEquals('000000', $style->getFont()->getColor()->getRGB()); self::assertEquals('000000', $style->getFont()->getColor()->getRGB());
self::assertEquals('000000', $style->getFill()->getEndColor()->getRGB()); self::assertEquals('000000', $style->getFill()->getEndColor()->getRGB());
self::assertEquals('FFFFFF', $style->getFill()->getstartColor()->getRGB()); self::assertEquals('FFFFFF', $style->getFill()->getstartColor()->getRGB());
$style = $firstSheet->getCell('C1')->getStyle();
self::assertEquals('f0f8ff', $style->getFont()->getColor()->getRGB());
self::assertEquals('eedfcc', $style->getFill()->getEndColor()->getRGB());
self::assertEquals('eedfcc', $style->getFill()->getstartColor()->getRGB());
} }
public function testCanApplyInlineFontStyles(): void public function testCanApplyInlineFontStyles(): void

View File

@ -0,0 +1,34 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BIFF5;
use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BIFF8;
use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BuiltIn;
use PHPUnit\Framework\TestCase;
class ColorMapTest extends TestCase
{
/**
* @dataProvider colorMapProvider
*/
public function testColorMap(int $index, string $expectedBiff5, string $expectedBiff8, string $expectedBuiltin): void
{
self::assertSame($expectedBiff5, BIFF5::lookup($index)['rgb']);
self::assertSame($expectedBiff8, BIFF8::lookup($index)['rgb']);
self::assertSame($expectedBuiltin, BuiltIn::lookup($index)['rgb']);
}
public function colorMapProvider(): array
{
return [
'default builtin' => [0x00, '000000', '000000', '000000'],
'non-default builtin' => [0x02, '000000', '000000', 'FF0000'],
'system window text color' => [0x40, '000000', '000000', '000000'],
'system window background color' => [0x41, '000000', '000000', 'FFFFFF'],
'same biff5/8' => [0x09, 'FFFFFF', 'FFFFFF', '000000'],
'different biff5/8' => [0x29, '69FFFF', 'CCFFFF', '000000'],
];
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xls\ErrorCode;
use PHPUnit\Framework\TestCase;
class ErrorCodeMapTest extends TestCase
{
/**
* @param bool|string $expected
*
* @dataProvider errorCodeMapProvider
*/
public function testErrorCode($expected, int $index): void
{
self::assertSame($expected, ErrorCode::lookup($index));
}
public function errorCodeMapProvider(): array
{
return [
[false, 0x01],
['#NULL!', 0x00],
['#DIV/0!', 0x07],
['#VALUE!', 0x0F],
['#REF!', 0x17],
['#NAME?', 0x1d],
['#NUM!', 0x24],
['#N/A', 0x2a],
];
}
}