diff --git a/src/PhpSpreadsheet/Reader/Xls.php b/src/PhpSpreadsheet/Reader/Xls.php index 90bd5db69..816e7698d 100644 --- a/src/PhpSpreadsheet/Reader/Xls.php +++ b/src/PhpSpreadsheet/Reader/Xls.php @@ -430,7 +430,7 @@ class Xls extends BaseReader */ public function canRead(string $filename): bool { - if (!File::testFileNoThrow($filename)) { + if (File::testFileNoThrow($filename) === false) { return false; } @@ -440,6 +440,9 @@ class Xls extends BaseReader // get excel data $ole->read($filename); + if ($ole->wrkbook === null) { + throw new Exception('The filename ' . $filename . ' is not recognised as a Spreadsheet file'); + } return true; } catch (PhpSpreadsheetException $e) { @@ -449,7 +452,7 @@ class Xls extends BaseReader public function setCodepage(string $codepage): void { - if (!CodePage::validate($codepage)) { + if (CodePage::validate($codepage) === false) { throw new PhpSpreadsheetException('Unknown codepage: ' . $codepage); } diff --git a/src/PhpSpreadsheet/Shared/OLERead.php b/src/PhpSpreadsheet/Shared/OLERead.php index 3d952a9c8..fcc963953 100644 --- a/src/PhpSpreadsheet/Shared/OLERead.php +++ b/src/PhpSpreadsheet/Shared/OLERead.php @@ -134,7 +134,7 @@ class OLERead $bbdBlocks = $this->numBigBlockDepotBlocks; - if ($this->numExtensionBlocks != 0) { + if ($this->numExtensionBlocks !== 0) { $bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS) / 4; } diff --git a/tests/PhpSpreadsheetTests/IOFactoryTest.php b/tests/PhpSpreadsheetTests/IOFactoryTest.php index 19722dc7f..966a12629 100644 --- a/tests/PhpSpreadsheetTests/IOFactoryTest.php +++ b/tests/PhpSpreadsheetTests/IOFactoryTest.php @@ -108,6 +108,33 @@ class IOFactoryTest extends TestCase ]; } + public function testIdentifyInvalid(): void + { + $file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc'; + + $this->expectException(ReaderException::class); + $this->expectExceptionMessage('Unable to identify a reader for this file'); + IOFactory::identify($file); + } + + public function testCreateInvalid(): void + { + $file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc'; + + $this->expectException(ReaderException::class); + $this->expectExceptionMessage('Unable to identify a reader for this file'); + IOFactory::createReaderForFile($file); + } + + public function testLoadInvalid(): void + { + $file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc'; + + $this->expectException(ReaderException::class); + $this->expectExceptionMessage('Unable to identify a reader for this file'); + IOFactory::load($file); + } + public function testFormatAsExpected(): void { $fileName = 'samples/templates/30template.xls'; diff --git a/tests/data/Reader/NotASpreadsheetFile.doc b/tests/data/Reader/NotASpreadsheetFile.doc new file mode 100644 index 000000000..10bf907ae Binary files /dev/null and b/tests/data/Reader/NotASpreadsheetFile.doc differ