Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/drop php71 #376

Merged
merged 5 commits into from
Feb 19, 2020
Merged
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
28 changes: 7 additions & 21 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ env:
- COMPOSER_ARGS="--no-interaction --prefer-source --no-suggest"
matrix:
include:
- php: 7.0
env:
- COLLECT_COVERAGE=true
- IGNORE_PLATFORMS=false
- RUN_PHPSTAN=true
- VALIDATE_CODING_STYLE=false
- php: 7.1
env:
- COLLECT_COVERAGE=true
- IGNORE_PLATFORMS=false
- RUN_PHPSTAN=true
- VALIDATE_CODING_STYLE=false
- php: 7.2
env:
- COLLECT_COVERAGE=true
Expand All @@ -34,29 +22,27 @@ matrix:
- VALIDATE_CODING_STYLE=false
- php: 7.4
env:
- COLLECT_COVERAGE=false
- COLLECT_COVERAGE=true
- IGNORE_PLATFORMS=false
- RUN_PHPSTAN=true
- VALIDATE_CODING_STYLE=false
- RUN_PHPSTAN=false
- IGNORE_PLATFORMS=true
- php: nightly
env:
- COLLECT_COVERAGE=false
- IGNORE_PLATFORMS=true
- RUN_PHPSTAN=true
- RUN_PHPSTAN=false
- VALIDATE_CODING_STYLE=false
allow_failures:
- php: nightly
fast_finish: true
before_install:
- travis_retry composer self-update
install:
- if [ "$IGNORE_PLATFORMS" == "true" ]; then travis_retry composer update $COMPOSER_ARGS
--ignore-platform-reqs; fi
- if [ "$IGNORE_PLATFORMS" == "false" ]; then travis_retry composer update $COMPOSER_ARGS;
fi
- if [ "$IGNORE_PLATFORMS" == "true" ]; then travis_retry composer update $COMPOSER_ARGS --ignore-platform-reqs; fi
- if [ "$IGNORE_PLATFORMS" == "false" ]; then travis_retry composer update $COMPOSER_ARGS; fi
script:
- if [ "$RUN_PHPSTAN" == "true" ]; then composer phpstan; fi
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi
- if [ "$RUN_PHPSTAN" == "true" ]; then composer phpstan; fi
- composer phpunit
after_script:
- if [ "$COLLECT_COVERAGE" == "true" ]; then wget https://scrutinizer-ci.com/ocular.phar
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
"source": "https://github.com/thephpleague/csv"
},
"require": {
"php" : "^7.0.10",
"ext-dom" : "*",
"php" : "^7.2",
"ext-json" : "*",
"ext-mbstring" : "*"
},
"require-dev": {
"ext-curl" : "*",
"friendsofphp/php-cs-fixer": "^2.12",
"phpunit/phpunit" : "^6.0",
"phpstan/phpstan": "^0.9.2",
"phpstan/phpstan-strict-rules": "^0.9.0",
"phpstan/phpstan-phpunit": "^0.9.4"
"friendsofphp/php-cs-fixer": "^2.16",
"phpunit/phpunit" : "^8.0",
"phpstan/phpstan": "^0.12.0",
"phpstan/phpstan-strict-rules": "^0.12.0",
"phpstan/phpstan-phpunit": "^0.12.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -69,7 +68,8 @@
"test": "Runs full test suite"
},
"suggest": {
"ext-iconv" : "Needed to ease transcoding CSV using iconv stream filters"
"ext-iconv" : "Needed to ease transcoding CSV using iconv stream filters",
"ext-dom" : "Need to use the XMLConverter class"
},
"extra": {
"branch-alias": {
Expand Down
2 changes: 1 addition & 1 deletion docs/9.0/connections/controls.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ If the submitted delimiter **is invalid or not found** in the document, `0` will

<p class="message-info">To detect the delimiters stats on the full CSV document you need to set <code>$limit</code> to <code>-1</code>.</p>
<p class="message-notice">This function only returns hints. Only the CSV providers will validate the real CSV delimiter character.</p>
<p class="message-warning">This function only test the delimiters you gave it.</p>
<p class="message-warning">This function only test the delimiters you gave it.</p>
2 changes: 1 addition & 1 deletion docs/9.0/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,4 @@ use League\Csv\Writer;

$csv = Writer::createFromPath('/path/to/file.csv');
$csv->addValidator(new ColumnConsistency(), 'columns_consistency');
~~~
~~~
20 changes: 18 additions & 2 deletions phpstan.src.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
parameters:
ignoreErrors:
reportUnmatchedIgnoredErrors: false
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
ignoreErrors:
- message: '#Variable method call on \$this\(League\\Csv\\XMLConverter\)#'
path: src/XMLConverter.php
- message: '#Variable method call on \$this\(League\\Csv\\Writer\)#'
path: src/Writer.php
- message: '#Variable method call on League\\Csv\\ResultSet#'
path: src/Reader.php
- message: '#Method League\\Csv\\Reader::__call\(\) has no return typehint specified.#'
path: src/Reader.php
- '#Access to an undefined property object::\$data.#'
- '#Unreachable statement - code above always terminates.#'
- message: '#Call to function is_resource\(\) with resource will always evaluate to true.#'
path: src/Stream.php
- message: '#Unsafe usage of new static\(\).#'
path: src/AbstractCsv.php
reportUnmatchedIgnoredErrors: false
23 changes: 11 additions & 12 deletions phpstan.tests.neon
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.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 \$stream 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.#'
- '#Parameter \#1 \$document of static method League\\Csv\\Polyfill\\EmptyEscapeParser::parse\(\) expects League\\Csv\\Stream\|SplFileObject, array given.#'
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- message: '#Parameter \#2 \$special_chars of class League\\Csv\\EscapeFormula constructor expects array<string>, array<int, stdClass> given.#'
path: tests/EscapeFormulaTest.php
- message: '#Parameter \#2 \$delimiters of function League\\Csv\\delimiter_detect expects array<string>, array<int, array\|string> given.#'
path: tests/DetectDelimiterTest.php
- message: '#Parameter \#1 \$document of static method League\\Csv\\Polyfill\\EmptyEscapeParser::parse\(\) expects League\\Csv\\Stream\|SplFileObject, stdClass given.#'
path: tests/Polyfill/EmptyEscapeParserTest.php
reportUnmatchedIgnoredErrors: true
checkMissingIterableValueType: false
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<logging>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/clover.xml"/>
</logging>
Expand Down
20 changes: 12 additions & 8 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ abstract class AbstractCsv implements ByteSequence
protected function __construct($document)
{
$this->document = $document;
list($this->delimiter, $this->enclosure, $this->escape) = $this->document->getCsvControl();
[$this->delimiter, $this->enclosure, $this->escape] = $this->document->getCsvControl();
$this->resetProperties();
}

/**
* Reset dynamic object properties to improve performance.
*/
protected function resetProperties()
protected function resetProperties(): void
{
}

Expand Down Expand Up @@ -280,7 +280,9 @@ public function chunk(int $length): Generator
$this->document->rewind();
$this->document->setFlags(0);
$this->document->fseek(strlen($input_bom));
foreach (str_split($this->output_bom.$this->document->fread($length), $length) as $chunk) {
/** @var array<int, string> $chunks */
$chunks = str_split($this->output_bom.$this->document->fread($length), $length);
foreach ($chunks as $chunk) {
yield $chunk;
}

Expand Down Expand Up @@ -334,7 +336,7 @@ public function output(string $filename = null): int

echo $this->output_bom;

return strlen($this->output_bom) + $this->document->fpassthru();
return strlen($this->output_bom) + (int) $this->document->fpassthru();
}

/**
Expand All @@ -346,7 +348,7 @@ public function output(string $filename = null): int
*
* @see https://tools.ietf.org/html/rfc6266#section-4.3
*/
protected function sendHeaders(string $filename)
protected function sendHeaders(string $filename): void
{
if (strlen($filename) != strcspn($filename, '\\/')) {
throw new InvalidArgument('The filename cannot contain the "/" and "\\" characters.');
Expand All @@ -357,10 +359,12 @@ protected function sendHeaders(string $filename)
$flag |= FILTER_FLAG_STRIP_HIGH;
}

$filenameFallback = str_replace('%', '', filter_var($filename, FILTER_SANITIZE_STRING, $flag));
/** @var string $filtered_name */
$filtered_name = filter_var($filename, FILTER_SANITIZE_STRING, $flag);
$filename_fallback = str_replace('%', '', $filtered_name);

$disposition = sprintf('attachment; filename="%s"', str_replace('"', '\\"', $filenameFallback));
if ($filename !== $filenameFallback) {
$disposition = sprintf('attachment; filename="%s"', str_replace('"', '\\"', $filename_fallback));
if ($filename !== $filename_fallback) {
$disposition .= sprintf("; filename*=utf-8''%s", rawurlencode($filename));
}

Expand Down
4 changes: 2 additions & 2 deletions src/CannotInsertRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CannotInsertRecord extends Exception
*/
public static function triggerOnInsertion(array $record): self
{
$exception = new static('Unable to write record to the CSV document');
$exception = new self('Unable to write record to the CSV document');
$exception->record = $record;

return $exception;
Expand All @@ -48,7 +48,7 @@ public static function triggerOnInsertion(array $record): self
*/
public static function triggerOnValidation(string $name, array $record): self
{
$exception = new static('Record validation failed');
$exception = new self('Record validation failed');
$exception->name = $name;
$exception->record = $record;

Expand Down
35 changes: 13 additions & 22 deletions src/CharsetConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@

use OutOfRangeException;
use php_user_filter;
use Traversable;
use TypeError;
use function array_combine;
use function array_map;
use function array_walk;
use function gettype;
use function in_array;
use function is_iterable;
use function is_numeric;
use function mb_convert_encoding;
use function mb_list_encodings;
Expand Down Expand Up @@ -85,7 +81,7 @@ public static function addTo(AbstractCsv $csv, string $input_encoding, string $o
/**
* Static method to register the class as a stream filter.
*/
public static function register()
public static function register(): void
{
$filtername = self::FILTERNAME.'.*';
if (!in_array($filtername, stream_get_filters(), true)) {
Expand Down Expand Up @@ -130,7 +126,7 @@ protected static function filterEncoding(string $encoding): string
/**
* {@inheritdoc}
*/
public function onCreate()
public function onCreate(): bool
{
$prefix = self::FILTERNAME.'.';
if (0 !== strpos($this->filtername, $prefix)) {
Expand All @@ -143,8 +139,8 @@ public function onCreate()
}

try {
$this->input_encoding = $this->filterEncoding($matches['input']);
$this->output_encoding = $this->filterEncoding($matches['output']);
$this->input_encoding = self::filterEncoding($matches['input']);
$this->output_encoding = self::filterEncoding($matches['output']);
} catch (OutOfRangeException $e) {
return false;
}
Expand All @@ -153,9 +149,12 @@ public function onCreate()
}

/**
* {@inheritdoc}
* @param resource $in
* @param resource $out
* @param int $consumed
* @param bool $closing
*/
public function filter($in, $out, &$consumed, $closing)
public function filter($in, $out, &$consumed, $closing): int
{
while ($res = stream_bucket_make_writeable($in)) {
$res->data = @mb_convert_encoding($res->data, $this->output_encoding, $this->input_encoding);
Expand All @@ -168,17 +167,9 @@ public function filter($in, $out, &$consumed, $closing)

/**
* Convert Csv records collection into UTF-8.
*
* @param array|Traversable $records
*
* @return array|Traversable
*/
public function convert($records)
public function convert(iterable $records): iterable
{
if (!is_iterable($records)) {
throw new TypeError(sprintf('%s() expects argument passed to be iterable, %s given', __METHOD__, gettype($records)));
}

if ($this->output_encoding === $this->input_encoding) {
return $records;
}
Expand Down Expand Up @@ -206,7 +197,7 @@ public function __invoke(array $record): array
* @param mixed $value
* @param mixed $offset
*/
protected function encodeField(&$value, &$offset)
protected function encodeField(&$value, &$offset): void
{
if (null !== $value && !is_numeric($value)) {
$value = mb_convert_encoding((string) $value, $this->output_encoding, $this->input_encoding);
Expand All @@ -222,7 +213,7 @@ protected function encodeField(&$value, &$offset)
*/
public function inputEncoding(string $encoding): self
{
$encoding = $this->filterEncoding($encoding);
$encoding = self::filterEncoding($encoding);
if ($encoding === $this->input_encoding) {
return $this;
}
Expand All @@ -238,7 +229,7 @@ public function inputEncoding(string $encoding): self
*/
public function outputEncoding(string $encoding): self
{
$encoding = $this->filterEncoding($encoding);
$encoding = self::filterEncoding($encoding);
if ($encoding === $this->output_encoding) {
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ColumnConsistency.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ColumnConsistency
/**
* New Instance.
*
* @throws OutOfRangeException if the column count is lesser than -1
* @throws Exception if the column count is lesser than -1
*/
public function __construct(int $columns_count = -1)
{
Expand Down
Loading