Skip to content

Commit

Permalink
Merge pull request #283 from lookyman/phpstan
Browse files Browse the repository at this point in the history
Static analysis with PHPStan
  • Loading branch information
nyamsprod committed Feb 21, 2018
2 parents 57858a6 + 3ec0750 commit 3c50253
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -29,7 +29,8 @@ install:

script:
- composer phpunit
- composer phpstan

after_script:
- if [ "$COLLECT_COVERAGE" == "true" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover build/clover.xml; fi
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi
13 changes: 11 additions & 2 deletions composer.json
Expand Up @@ -24,7 +24,10 @@
"require-dev": {
"ext-curl" : "*",
"friendsofphp/php-cs-fixer": "^2.0",
"phpunit/phpunit" : "^6.0"
"phpunit/phpunit" : "^6.0",
"phpstan/phpstan": "^0.9.2",
"phpstan/phpstan-strict-rules": "^0.9.0",
"phpstan/phpstan-phpunit": "^0.9.4"
},
"autoload": {
"psr-4": {
Expand All @@ -40,7 +43,13 @@
"scripts": {
"test": "phpunit --coverage-text; php-cs-fixer fix -v --diff --dry-run --allow-risky=yes;",
"phpunit": "phpunit --coverage-text",
"phpcs": "php-cs-fixer fix -v --diff --dry-run --allow-risky=yes;"
"phpcs": "php-cs-fixer fix -v --diff --dry-run --allow-risky=yes;",
"phpstan-src": "phpstan analyse -l 7 -c phpstan.src.neon src",
"phpstan-tests": "phpstan analyse -l 7 -c phpstan.tests.neon tests",
"phpstan": [
"@phpstan-src",
"@phpstan-tests"
]
},
"suggest": {
"ext-iconv" : "Needed to ease transcoding CSV using iconv stream filters"
Expand Down
2 changes: 2 additions & 0 deletions phpstan.src.neon
@@ -0,0 +1,2 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
15 changes: 15 additions & 0 deletions phpstan.tests.neon
@@ -0,0 +1,15 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-phpunit/strictRules.neon
parameters:
ignoreErrors:
- '#Function xdebug_get_headers not found.#'
- '#Call to function is_iterable\(\) will always evaluate to false.#'
- '#Parameter \#1 \$stream of static method League\\Csv\\AbstractCsv::createFromStream\(\) expects resource, string given.#'
- '#Parameter \#2 \$special_chars of class League\\Csv\\EscapeFormula constructor expects array<string>, array<int, stdClass> given.#'
- '#Parameter \#1 \$resource of class League\\Csv\\Stream constructor expects resource, string given.#'
- '#Parameter \#1 \$records of method League\\Csv\\CharsetConverter::convert\(\) expects array|Traversable, string given.#'
- '#Parameter \#2 \$delimiters of function League\\Csv\\delimiter_detect expects array<string>, array<int, array|string> given.#'
reportUnmatchedIgnoredErrors: false
2 changes: 1 addition & 1 deletion src/AbstractCsv.php
Expand Up @@ -334,7 +334,7 @@ public function output(string $filename = null): int
*
* Adapted from Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition
*
* @param string|null $filename CSV disposition name
* @param string $filename CSV disposition name
*
* @throws Exception if the submitted header is invalid according to RFC 6266
*
Expand Down
10 changes: 5 additions & 5 deletions src/CharsetConverter.php
Expand Up @@ -81,7 +81,7 @@ public static function addTo(AbstractCsv $csv, string $input_encoding, string $o
public static function register()
{
$filtername = self::FILTERNAME.'.*';
if (!in_array($filtername, stream_get_filters())) {
if (!in_array($filtername, stream_get_filters(), true)) {
stream_filter_register($filtername, __CLASS__);
}
}
Expand Down Expand Up @@ -208,17 +208,17 @@ public function __invoke(array $record): array
/**
* Walker method to convert the offset and the value of a CSV record field
*
* @param string|null &$value
* @param string|int &$offset
* @param string|null $value
* @param string|int $offset
*/
protected function encodeField(&$value, &$offset)
{
if (null !== $value) {
$value = mb_convert_encoding((string) $value, $this->output_encoding, $this->input_encoding);
$value = mb_convert_encoding($value, $this->output_encoding, $this->input_encoding);
}

if (!is_int($offset)) {
$offset = mb_convert_encoding((string) $offset, $this->output_encoding, $this->input_encoding);
$offset = mb_convert_encoding($offset, $this->output_encoding, $this->input_encoding);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/EncloseField.php
Expand Up @@ -75,7 +75,7 @@ public static function getFiltername(): string
*/
public static function register()
{
if (!in_array(self::FILTERNAME, stream_get_filters())) {
if (!in_array(self::FILTERNAME, stream_get_filters(), true)) {
stream_filter_register(self::FILTERNAME, __CLASS__);
}
}
Expand All @@ -88,7 +88,7 @@ public static function register()
*
* @throws InvalidArgumentException if the sequence is malformed
*
* @return AbstractCsv
* @return Writer
*/
public static function addTo(Writer $csv, string $sequence): Writer
{
Expand Down
2 changes: 1 addition & 1 deletion src/RFC4180Field.php
Expand Up @@ -127,7 +127,7 @@ public static function addFormatterTo(Writer $csv, string $whitespace_replace):
*/
public static function register()
{
if (!in_array(self::FILTERNAME, stream_get_filters())) {
if (!in_array(self::FILTERNAME, stream_get_filters(), true)) {
stream_filter_register(self::FILTERNAME, __CLASS__);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Statement.php
Expand Up @@ -89,7 +89,7 @@ public function orderBy(callable $callable): self
/**
* Set LimitIterator Offset
*
* @param $offset
* @param int $offset
*
* @throws Exception if the offset is lesser than 0
*
Expand Down
10 changes: 5 additions & 5 deletions src/Writer.php
Expand Up @@ -115,7 +115,8 @@ public function insertAll($records): int
/**
* Adds a single record to a CSV document
*
* @param string[] $record an array
* @param array $record An array consisting of nulls or values that can be converted to string (scalar string, int,
* float, objects implementing __toString() method.
*
* @throws CannotInsertRecord If the record can not be inserted
*
Expand All @@ -126,11 +127,10 @@ public function insertOne(array $record): int
$record = array_reduce($this->formatters, [$this, 'formatRecord'], $record);
$this->validateRecord($record);
$bytes = $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape);
if (!$bytes) {
throw CannotInsertRecord::triggerOnInsertion($record);
if (false !== $bytes) {
return $bytes + $this->consolidate();
}

return $bytes + $this->consolidate();
throw CannotInsertRecord::triggerOnInsertion($record);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/ReaderTest.php
Expand Up @@ -76,7 +76,7 @@ public function testGetIterator()

$this->csv->setHeaderOffset(null);
foreach ($this->csv->getRecords() as $record) {
$this->assertTrue(in_array(count($record), [3, 4]));
$this->assertTrue(in_array(count($record), [3, 4], true));
}
}

Expand Down
4 changes: 1 addition & 3 deletions tests/ResultSetTest.php
Expand Up @@ -160,7 +160,7 @@ public function testIntervalThrowException()
public function testFilter()
{
$func = function ($row) {
return !in_array('jane', $row);
return !in_array('jane', $row, true);
};

$this->assertNotContains(
Expand Down Expand Up @@ -230,8 +230,6 @@ public function invalidFieldNameProvider()
* @covers ::fetchColumn
* @covers ::getColumnIndexByKey
* @covers League\Csv\MapIterator
*
* @param int|string $field
*/
public function testFetchColumnTriggersOutOfRangeException()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/StreamWrapper.php
Expand Up @@ -23,7 +23,7 @@ final class StreamWrapper

public static function register()
{
if (!in_array(self::PROTOCOL, stream_get_wrappers())) {
if (!in_array(self::PROTOCOL, stream_get_wrappers(), true)) {
stream_wrapper_register(self::PROTOCOL, __CLASS__);
}
}
Expand Down

0 comments on commit 3c50253

Please sign in to comment.