PHPStan Level 4

This commit is contained in:
Adrien Loison 2022-01-16 23:09:12 +01:00
parent ea0a67d283
commit 6f1b67b39d
23 changed files with 32 additions and 43 deletions

View File

@ -1,5 +1,5 @@
parameters: parameters:
level: 3 level: 4
paths: paths:
- src - src
- tests - tests

View File

@ -66,7 +66,7 @@ class Style
/** @var bool Whether the wrap text property was set */ /** @var bool Whether the wrap text property was set */
private $hasSetWrapText = false; private $hasSetWrapText = false;
/** @var Border */ /** @var Border|null */
private $border; private $border;
/** @var bool Whether border properties should be applied */ /** @var bool Whether border properties should be applied */
@ -78,7 +78,7 @@ class Style
/** @var bool */ /** @var bool */
private $hasSetBackgroundColor = false; private $hasSetBackgroundColor = false;
/** @var string Format */ /** @var string|null Format */
private $format; private $format;
/** @var bool */ /** @var bool */
@ -110,7 +110,7 @@ class Style
} }
/** /**
* @return Border * @return Border|null
*/ */
public function getBorder() public function getBorder()
{ {
@ -467,7 +467,7 @@ class Style
} }
/** /**
* @return string * @return string|null
*/ */
public function getFormat() public function getFormat()
{ {

View File

@ -103,7 +103,7 @@ class GlobalFunctionsHelper
* @param array $fields * @param array $fields
* @param string|null $delimiter * @param string|null $delimiter
* @param string|null $enclosure * @param string|null $enclosure
* @return int * @return int|false
*/ */
public function fputcsv($handle, array $fields, $delimiter = null, $enclosure = null) public function fputcsv($handle, array $fields, $delimiter = null, $enclosure = null)
{ {

View File

@ -142,7 +142,7 @@ class Reader extends ReaderAbstract
*/ */
protected function closeReader() protected function closeReader()
{ {
if ($this->filePointer) { if (is_resource($this->filePointer)) {
$this->globalFunctionsHelper->fclose($this->filePointer); $this->globalFunctionsHelper->fclose($this->filePointer);
} }

View File

@ -21,7 +21,7 @@ class RowIterator implements IteratorInterface
*/ */
public const MAX_READ_BYTES_PER_LINE = 0; public const MAX_READ_BYTES_PER_LINE = 0;
/** @var resource Pointer to the CSV file to read */ /** @var resource|null Pointer to the CSV file to read */
protected $filePointer; protected $filePointer;
/** @var int Number of read rows */ /** @var int Number of read rows */

View File

@ -69,7 +69,7 @@ class Reader extends ReaderAbstract
*/ */
protected function closeReader() protected function closeReader()
{ {
if ($this->zip) { if ($this->zip !== null) {
$this->zip->close(); $this->zip->close();
} }
} }

View File

@ -233,7 +233,7 @@ abstract class ReaderAbstract implements ReaderInterface
$this->closeReader(); $this->closeReader();
$sheetIterator = $this->getConcreteSheetIterator(); $sheetIterator = $this->getConcreteSheetIterator();
if ($sheetIterator) { if ($sheetIterator !== null) {
$sheetIterator->end(); $sheetIterator->end();
} }

View File

@ -21,7 +21,7 @@ class ManagerFactory
/** @var CachingStrategyFactory */ /** @var CachingStrategyFactory */
private $cachingStrategyFactory; private $cachingStrategyFactory;
/** @var WorkbookRelationshipsManager */ /** @var WorkbookRelationshipsManager|null */
private $cachedWorkbookRelationshipsManager; private $cachedWorkbookRelationshipsManager;
/** /**

View File

@ -84,7 +84,7 @@ class CachingStrategyFactory
$memoryAvailable = $this->getMemoryLimitInKB(); $memoryAvailable = $this->getMemoryLimitInKB();
if ($memoryAvailable === -1) { if ((int) $memoryAvailable === -1) {
// if cannot get memory limit or if memory limit set as unlimited, don't trust and play safe // if cannot get memory limit or if memory limit set as unlimited, don't trust and play safe
$isInMemoryStrategyUsageSafe = ($sharedStringsUniqueCount < self::MAX_NUM_STRINGS_PER_TEMP_FILE); $isInMemoryStrategyUsageSafe = ($sharedStringsUniqueCount < self::MAX_NUM_STRINGS_PER_TEMP_FILE);
} else { } else {

View File

@ -242,7 +242,7 @@ class SharedStringsManager
*/ */
public function cleanup() public function cleanup()
{ {
if ($this->cachingStrategy) { if ($this->cachingStrategy !== null) {
$this->cachingStrategy->clearCache(); $this->cachingStrategy->clearCache();
} }
} }

View File

@ -60,10 +60,10 @@ class StyleManager
/** @var array Array containing the IDs of built-in number formats indicating a date */ /** @var array Array containing the IDs of built-in number formats indicating a date */
protected $builtinNumFmtIdIndicatingDates; protected $builtinNumFmtIdIndicatingDates;
/** @var array Array containing a mapping NUM_FMT_ID => FORMAT_CODE */ /** @var array|null Array containing a mapping NUM_FMT_ID => FORMAT_CODE */
protected $customNumberFormats; protected $customNumberFormats;
/** @var array Array containing a mapping STYLE_ID => [STYLE_ATTRIBUTES] */ /** @var array|null Array containing a mapping STYLE_ID => [STYLE_ATTRIBUTES] */
protected $stylesAttributes; protected $stylesAttributes;
/** @var array Cache containing a mapping NUM_FMT_ID => IS_DATE_FORMAT. Used to avoid lots of recalculations */ /** @var array Cache containing a mapping NUM_FMT_ID => IS_DATE_FORMAT. Used to avoid lots of recalculations */

View File

@ -34,7 +34,7 @@ class WorkbookRelationshipsManager
/** @var InternalEntityFactory Factory to create entities */ /** @var InternalEntityFactory Factory to create entities */
private $entityFactory; private $entityFactory;
/** @var array Cache of the already read workbook relationships: [TYPE] => [FILE_NAME] */ /** @var array|null Cache of the already read workbook relationships: [TYPE] => [FILE_NAME] */
private $cachedWorkbookRelationships; private $cachedWorkbookRelationships;
/** /**

View File

@ -119,11 +119,11 @@ class Reader extends ReaderAbstract
*/ */
protected function closeReader() protected function closeReader()
{ {
if ($this->zip) { if ($this->zip !== null) {
$this->zip->close(); $this->zip->close();
} }
if ($this->sharedStringsManager) { if ($this->sharedStringsManager !== null) {
$this->sharedStringsManager->cleanup(); $this->sharedStringsManager->cleanup();
} }
} }

View File

@ -88,10 +88,10 @@ class StyleMerger
if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) { if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) {
$styleToUpdate->setCellAlignment($baseStyle->getCellAlignment()); $styleToUpdate->setCellAlignment($baseStyle->getCellAlignment());
} }
if (!$style->getBorder() && $baseStyle->shouldApplyBorder()) { if ($style->getBorder() === null && $baseStyle->shouldApplyBorder()) {
$styleToUpdate->setBorder($baseStyle->getBorder()); $styleToUpdate->setBorder($baseStyle->getBorder());
} }
if (!$style->getFormat() && $baseStyle->shouldApplyFormat()) { if ($style->getFormat() === null && $baseStyle->shouldApplyFormat()) {
$styleToUpdate->setFormat($baseStyle->getFormat()); $styleToUpdate->setFormat($baseStyle->getFormat());
} }
if (!$style->shouldApplyBackgroundColor() && $baseStyle->shouldApplyBackgroundColor()) { if (!$style->shouldApplyBackgroundColor() && $baseStyle->shouldApplyBackgroundColor()) {

View File

@ -23,7 +23,7 @@ use Box\Spout\Writer\Exception\WriterException;
*/ */
abstract class WorkbookManagerAbstract implements WorkbookManagerInterface abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
{ {
/** @var Workbook The workbook to manage */ /** @var Workbook|null The workbook to manage */
protected $workbook; protected $workbook;
/** @var OptionsManagerInterface */ /** @var OptionsManagerInterface */
@ -92,7 +92,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
abstract protected function getWorksheetFilePath(Sheet $sheet); abstract protected function getWorksheetFilePath(Sheet $sheet);
/** /**
* @return Workbook * @return Workbook|null
*/ */
public function getWorkbook() public function getWorkbook()
{ {

View File

@ -17,7 +17,7 @@ use Box\Spout\Writer\Exception\WriterException;
interface WorkbookManagerInterface interface WorkbookManagerInterface
{ {
/** /**
* @return Workbook * @return Workbook|null
*/ */
public function getWorkbook(); public function getWorkbook();

View File

@ -169,7 +169,7 @@ abstract class WriterAbstract implements WriterInterface
*/ */
protected function throwIfFilePointerIsNotAvailable() protected function throwIfFilePointerIsNotAvailable()
{ {
if (!$this->filePointer) { if (!is_resource($this->filePointer)) {
throw new IOException('File pointer has not be opened'); throw new IOException('File pointer has not be opened');
} }
} }

View File

@ -9,7 +9,6 @@ use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface; use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface;
use Box\Spout\Writer\Exception\SheetNotFoundException; use Box\Spout\Writer\Exception\SheetNotFoundException;
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
@ -25,7 +24,7 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
/** @var ManagerFactoryInterface */ /** @var ManagerFactoryInterface */
private $managerFactory; private $managerFactory;
/** @var WorkbookManagerInterface */ /** @var WorkbookManagerInterface|null */
private $workbookManager; private $workbookManager;
/** /**
@ -66,7 +65,7 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
*/ */
protected function openWriter() protected function openWriter()
{ {
if (!$this->workbookManager) { if ($this->workbookManager === null) {
$this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager); $this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager);
$this->workbookManager->addNewSheetAndMakeItCurrent(); $this->workbookManager->addNewSheetAndMakeItCurrent();
} }
@ -85,7 +84,6 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
$externalSheets = []; $externalSheets = [];
$worksheets = $this->workbookManager->getWorksheets(); $worksheets = $this->workbookManager->getWorksheets();
/** @var Worksheet $worksheet */
foreach ($worksheets as $worksheet) { foreach ($worksheets as $worksheet) {
$externalSheets[] = $worksheet->getExternalSheet(); $externalSheets[] = $worksheet->getExternalSheet();
} }
@ -143,7 +141,7 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
*/ */
protected function throwIfWorkbookIsNotAvailable() protected function throwIfWorkbookIsNotAvailable()
{ {
if (!$this->workbookManager->getWorkbook()) { if ($this->workbookManager->getWorkbook() === null) {
throw new WriterNotOpenedException('The writer must be opened before performing this action.'); throw new WriterNotOpenedException('The writer must be opened before performing this action.');
} }
} }
@ -162,7 +160,7 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
*/ */
protected function closeWriter() protected function closeWriter()
{ {
if ($this->workbookManager) { if ($this->workbookManager !== null) {
$this->workbookManager->close($this->filePointer); $this->workbookManager->close($this->filePointer);
} }
} }

View File

@ -91,8 +91,7 @@ class ManagerFactory implements ManagerFactoryInterface
$styleMerger, $styleMerger,
$sharedStringsManager, $sharedStringsManager,
$stringsEscaper, $stringsEscaper,
$stringsHelper, $stringsHelper
$this->entityFactory
); );
} }

View File

@ -59,7 +59,7 @@ EOD;
*/ */
protected function throwIfSharedStringsFilePointerIsNotAvailable() protected function throwIfSharedStringsFilePointerIsNotAvailable()
{ {
if (!$this->sharedStringsFilePointer) { if (!is_resource($this->sharedStringsFilePointer)) {
throw new IOException('Unable to open shared strings file for writing.'); throw new IOException('Unable to open shared strings file for writing.');
} }
} }

View File

@ -10,7 +10,6 @@ use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\Escaper\XLSX as XLSXEscaper; use Box\Spout\Common\Helper\Escaper\XLSX as XLSXEscaper;
use Box\Spout\Common\Helper\StringHelper; use Box\Spout\Common\Helper\StringHelper;
use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Creator\InternalEntityFactory;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Helper\CellHelper; use Box\Spout\Writer\Common\Helper\CellHelper;
@ -60,9 +59,6 @@ EOD;
/** @var StringHelper String helper */ /** @var StringHelper String helper */
private $stringHelper; private $stringHelper;
/** @var InternalEntityFactory Factory to create entities */
private $entityFactory;
/** /**
* WorksheetManager constructor. * WorksheetManager constructor.
* *
@ -73,7 +69,6 @@ EOD;
* @param SharedStringsManager $sharedStringsManager * @param SharedStringsManager $sharedStringsManager
* @param XLSXEscaper $stringsEscaper * @param XLSXEscaper $stringsEscaper
* @param StringHelper $stringHelper * @param StringHelper $stringHelper
* @param InternalEntityFactory $entityFactory
*/ */
public function __construct( public function __construct(
OptionsManagerInterface $optionsManager, OptionsManagerInterface $optionsManager,
@ -82,8 +77,7 @@ EOD;
StyleMerger $styleMerger, StyleMerger $styleMerger,
SharedStringsManager $sharedStringsManager, SharedStringsManager $sharedStringsManager,
XLSXEscaper $stringsEscaper, XLSXEscaper $stringsEscaper,
StringHelper $stringHelper, StringHelper $stringHelper
InternalEntityFactory $entityFactory
) { ) {
$this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS); $this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS);
$this->rowManager = $rowManager; $this->rowManager = $rowManager;
@ -92,7 +86,6 @@ EOD;
$this->sharedStringsManager = $sharedStringsManager; $this->sharedStringsManager = $sharedStringsManager;
$this->stringsEscaper = $stringsEscaper; $this->stringsEscaper = $stringsEscaper;
$this->stringHelper = $stringHelper; $this->stringHelper = $stringHelper;
$this->entityFactory = $entityFactory;
} }
/** /**

View File

@ -193,7 +193,7 @@ class WriterTest extends TestCase
* @param string $fieldDelimiter * @param string $fieldDelimiter
* @param string $fieldEnclosure * @param string $fieldEnclosure
* @param bool $shouldAddBOM * @param bool $shouldAddBOM
* @return string|null * @return string
*/ */
private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"', $shouldAddBOM = true) private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"', $shouldAddBOM = true)
{ {

View File

@ -475,7 +475,6 @@ class WriterWithStyleTest extends TestCase
$borderParts = $borderNode->childNodes; $borderParts = $borderNode->childNodes;
$ordering = []; $ordering = [];
/** @var \DOMText $part */
foreach ($borderParts as $part) { foreach ($borderParts as $part) {
if ($part instanceof \DOMElement) { if ($part instanceof \DOMElement) {
$ordering[] = $part->nodeName; $ordering[] = $part->nodeName;