Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Add phpstan #830

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ matrix:
include:
- php: 7.2
env: WITH_PHPUNIT=true
- php: 7.2
env: WITH_PHPSTAN=true
- php: 7.3
env: WITH_PHPUNIT=true
- php: 7.3
env: WITH_PHPUNIT=true WITH_COVERAGE=true
- php: 7.4
env: WITH_PHPUNIT=true
- php: 8.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removing PHP 8? I think it's important to keep it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env: WITH_PHPUNIT=true


cache:
Expand All @@ -30,6 +30,9 @@ before_install:
if [[ "$WITH_CS" != "true" ]]; then
composer remove friendsofphp/php-cs-fixer --dev --no-update
fi
if [[ "$WITH_PHPSTAN" != "true" ]]; then
composer remove phpstan/phpstan --dev --no-update
fi
- composer validate

install:
Expand All @@ -44,6 +47,10 @@ script:
if [[ "$WITH_CS" == "true" ]]; then
vendor/bin/php-cs-fixer fix --config=.php_cs.dist --verbose --diff --dry-run --diff-format=udiff
fi
- |
if [[ "$WITH_PHPSTAN" == "true" ]]; then
vendor/bin/phpstan
fi
- |
if [[ "$WITH_PHPUNIT" == "true" ]]; then
if [[ "$WITH_COVERAGE" == "true" ]]; then
Expand Down
23 changes: 22 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,28 @@ This will print a diff of proposed code style changes. To apply these suggestion
vendor/bin/php-cs-fixer fix --config=.php_cs.dist
```

### Step 9: Send the pull request
You can also use

```
composer phpcs
```

### Step 9: Fix phpstan

Run the following command to analyse source code.
Make sure to fix all errors before submitting a pull request.

```
vendor/bin/phpstan analyse
```

You can also use

```
composer phpstan
```

### Step 10: Send the pull request

Send the pull request from your feature branch to us. Be sure to include a description that lets us know what work you did.

Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"require-dev": {
"phpunit/phpunit": "^8",
"friendsofphp/php-cs-fixer": "^2"
"friendsofphp/php-cs-fixer": "^2",
"phpstan/phpstan": "^0.12.98"
},
"suggest": {
"ext-iconv": "To handle non UTF-8 CSV files (if \"php-intl\" is not already installed or is too limited)",
Expand All @@ -30,6 +31,11 @@
"Box\\Spout\\": "src/Spout"
}
},
"scripts": {
"phpunit": "vendor/bin/phpunit",
"phpstan": "vendor/bin/phpstan analyse",
"phpcs": "vendor/bin/php-cs-fixer fix --config=.php_cs.dist"
},
"extra": {
"branch-alias": {
"dev-master": "3.1.x-dev"
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 7
paths:
- src
- tests
2 changes: 1 addition & 1 deletion src/Spout/Autoloader/Psr4Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Psr4Autoloader
* An associative array where the key is a namespace prefix and the value
* is an array of base directories for classes in that namespace.
*
* @var array
* @var array<string, array>
*/
protected $prefixes = [];

Expand Down
6 changes: 3 additions & 3 deletions src/Spout/Common/Entity/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __construct($value, Style $style = null)
/**
* @param mixed|null $value
*/
public function setValue($value)
public function setValue($value) : void
{
$this->value = $value;
$this->type = $this->detectType($value);
Expand All @@ -102,7 +102,7 @@ public function getValueEvenIfError()
/**
* @param Style|null $style
*/
public function setStyle($style)
public function setStyle($style) : void
{
$this->style = $style ?: new Style();
}
Expand All @@ -126,7 +126,7 @@ public function getType()
/**
* @param int $type
*/
public function setType($type)
public function setType($type) : void
{
$this->type = $type;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Spout/Common/Entity/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public function getNumCells()
return 0;
}

return \max(\array_keys($this->cells)) + 1;
/** @var int $highest */
$highest = \max(\array_keys($this->cells));

return $highest + 1;
}

/**
Expand All @@ -118,7 +121,7 @@ public function setStyle($style)
}

/**
* @return array The row values, as array
* @return array<mixed> The row values, as array
*/
public function toArray()
{
Expand Down
8 changes: 4 additions & 4 deletions src/Spout/Common/Entity/Style/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class Border
const WIDTH_MEDIUM = 'medium';
const WIDTH_THICK = 'thick';

/** @var array A list of BorderPart objects for this border. */
/** @var array<BorderPart> A list of BorderPart objects for this border. */
private $parts = [];

/**
* @param array $borderParts
* @param array<BorderPart> $borderParts
*/
public function __construct(array $borderParts = [])
{
Expand All @@ -52,7 +52,7 @@ public function hasPart($name)
}

/**
* @return array
* @return array<BorderPart>
*/
public function getParts()
{
Expand All @@ -61,7 +61,7 @@ public function getParts()

/**
* Set BorderParts
* @param array $parts
* @param array<BorderPart> $parts
* @return void
*/
public function setParts($parts)
Expand Down
12 changes: 6 additions & 6 deletions src/Spout/Common/Entity/Style/BorderPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BorderPart
protected $width;

/**
* @var array Allowed style constants for parts.
* @var array<string> Allowed style constants for parts.
*/
protected static $allowedStyles = [
'none',
Expand All @@ -43,7 +43,7 @@ class BorderPart
];

/**
* @var array Allowed names constants for border parts.
* @var array<string> Allowed names constants for border parts.
*/
protected static $allowedNames = [
'left',
Expand All @@ -53,7 +53,7 @@ class BorderPart
];

/**
* @var array Allowed width constants for border parts.
* @var array<string> Allowed width constants for border parts.
*/
protected static $allowedWidths = [
'thin',
Expand Down Expand Up @@ -159,23 +159,23 @@ public function setWidth($width)
}

/**
* @return array
* @return array<string>
*/
public static function getAllowedStyles()
{
return self::$allowedStyles;
}

/**
* @return array
* @return array<string>
*/
public static function getAllowedNames()
{
return self::$allowedNames;
}

/**
* @return array
* @return array<string>
*/
public static function getAllowedWidths()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Spout/Common/Entity/Style/CellAlignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ abstract class CellAlignment
const CENTER = 'center';
const JUSTIFY = 'justify';

/**
* @var array<string, int>
*/
private static $VALID_ALIGNMENTS = [
self::LEFT => 1,
self::RIGHT => 1,
Expand Down
4 changes: 2 additions & 2 deletions src/Spout/Common/Entity/Style/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Style
/** @var bool Whether the wrap text property was set */
private $hasSetWrapText = false;

/** @var Border */
/** @var Border|null */
private $border;

/** @var bool Whether border properties should be applied */
Expand Down Expand Up @@ -110,7 +110,7 @@ public function setId($id)
}

/**
* @return Border
* @return Border|null
*/
public function getBorder()
{
Expand Down
10 changes: 5 additions & 5 deletions src/Spout/Common/Helper/CellTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class CellTypeHelper
{
/**
* @param $value
* @param mixed $value
* @return bool Whether the given value is considered "empty"
*/
public static function isEmpty($value)
Expand All @@ -18,7 +18,7 @@ public static function isEmpty($value)
}

/**
* @param $value
* @param mixed $value
* @return bool Whether the given value is a non empty string
*/
public static function isNonEmptyString($value)
Expand All @@ -30,7 +30,7 @@ public static function isNonEmptyString($value)
* Returns whether the given value is numeric.
* A numeric value is from type "integer" or "double" ("float" is not returned by gettype).
*
* @param $value
* @param mixed $value
* @return bool Whether the given value is numeric
*/
public static function isNumeric($value)
Expand All @@ -44,7 +44,7 @@ public static function isNumeric($value)
* Returns whether the given value is boolean.
* "true"/"false" and 0/1 are not booleans.
*
* @param $value
* @param mixed $value
* @return bool Whether the given value is boolean
*/
public static function isBoolean($value)
Expand All @@ -55,7 +55,7 @@ public static function isBoolean($value)
/**
* Returns whether the given value is a DateTime or DateInterval object.
*
* @param $value
* @param mixed $value
* @return bool Whether the given value is a DateTime or DateInterval object
*/
public static function isDateTimeOrDateInterval($value)
Expand Down
4 changes: 2 additions & 2 deletions src/Spout/Common/Helper/EncodingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EncodingHelper
/** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
protected $globalFunctionsHelper;

/** @var array Map representing the encodings supporting BOMs (key) and their associated BOM (value) */
/** @var array<string, string> Map representing the encodings supporting BOMs (key) and their associated BOM (value) */
protected $supportedEncodingsWithBom;

/**
Expand Down Expand Up @@ -143,7 +143,7 @@ protected function attemptConversion($string, $sourceEncoding, $targetEncoding)
throw new EncodingConversionException("The conversion from $sourceEncoding to $targetEncoding is not supported. Please install \"iconv\" or \"PHP Intl\".");
}

if ($convertedString === false) {
if (!is_string($convertedString)) {
throw new EncodingConversionException("The conversion from $sourceEncoding to $targetEncoding failed.");
}

Expand Down
1 change: 1 addition & 0 deletions src/Spout/Common/Helper/Escaper/XLSX.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class XLSX implements EscaperInterface

/**
* Initializes the control characters if not already done
* @return void
*/
protected function initIfNeeded()
{
Expand Down
6 changes: 5 additions & 1 deletion src/Spout/Common/Helper/FileSystemHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class FileSystemHelper implements FileSystemHelperInterface
*/
public function __construct(string $baseFolderPath)
{
$this->baseFolderRealPath = \realpath($baseFolderPath);
/** @var string $realPath */
$realPath = \realpath($baseFolderPath);

$this->baseFolderRealPath = $realPath;
}

/**
Expand Down Expand Up @@ -127,6 +130,7 @@ protected function throwIfOperationNotInBaseFolder(string $operationFolderPath)
if (!$this->baseFolderRealPath) {
throw new IOException("The base folder path is invalid: {$this->baseFolderRealPath}");
}
/** @var string $operationFolderRealPath */
$isInBaseFolder = (\strpos($operationFolderRealPath, $this->baseFolderRealPath) === 0);
if (!$isInBaseFolder) {
throw new IOException("Cannot perform I/O operation outside of the base folder: {$this->baseFolderRealPath}");
Expand Down
Loading