Skip to content

Commit

Permalink
Support PHP 8.2 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-m authored Jan 18, 2024
1 parent fde3028 commit 1adc0b3
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ workflows:
- matrix-conditions:
matrix:
parameters:
version: ["8.1", "8.0", "7.4"]
version: ["8.2", "8.1", "8.0", "7.4"]
install-flags: ["", "--prefer-lowest"]
12 changes: 12 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Exclude build/test files from archive
/.circleci export-ignore
/test export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpcs.xml export-ignore
/phpunit.xml export-ignore
/rector.php export-ignore

# Configure diff output for .php and .phar files.
*.php diff=php
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"php": ">=7.4",
"ext-json": "*",
"fmizzell/maquina": "^1.1.1",
"getdkan/contracts": "^1.1.1"
"getdkan/contracts": "^1.1.2"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"rector/rector": "^0.15.17",
"squizlabs/php_codesniffer": "^3.7"
"squizlabs/php_codesniffer": "^3.7",
"symfony/phpunit-bridge": "^7.0"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 12 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" verbose="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
verbose="false">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
<directory>src</directory>
</include>
</coverage>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
<php>
<!-- Don't fail for external dependencies. -->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
</php>
<testsuites>
<testsuite name="all">
<directory suffix="Test.php" phpVersion="7.2" phpVersionOperator="&gt;=">test</directory>
<directory>test</directory>
</testsuite>
</testsuites>
</phpunit>
32 changes: 28 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,44 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/test',
__DIR__ . '/src',
__DIR__ . '/test',
]);

// Our base version of PHP.
$rectorConfig->phpVersion(PhpVersion::PHP_74);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
SetList::PHP_82,
// Please no dead code or unneeded variables.
SetList::DEAD_CODE,
// Try to figure out type hints.
SetList::TYPE_DECLARATION,
]);

$rectorConfig->skip([
// Don't throw errors on JSON parse problems. Yet.
// @todo Throw errors and deal with them appropriately.
JsonThrowOnErrorRector::class,
// We like our tags. Please don't remove them.
RemoveUselessParamTagRector::class,
RemoveUselessReturnTagRector::class,
RemoveUselessVarTagRector::class,
ArrayShapeFromConstantArrayReturnRector::class,
AddMethodCallBasedStrictParamTypeRector::class,
]);

$rectorConfig->importNames();
$rectorConfig->importShortClasses(false);
};
24 changes: 13 additions & 11 deletions src/Parser/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ class Csv implements ParserInterface, \JsonSerializable
private $fields;
private string $field;

/**
* @var sm
*/
public $machine;

private $lastCharType;
private bool $quoted = false;

private bool $trailingDelimiter = false;

public function activateTrailingDelimiter()
public function activateTrailingDelimiter(): void
{
$this->trailingDelimiter = true;
}

public static function getParser($delimiter = ",", $quote = '"', $escape = "\\", $record_end = ["\n", "\r"])
public static function getParser($delimiter = ",", $quote = '"', $escape = "\\", $record_end = ["\n", "\r"]): self
{
return new self($delimiter, $quote, $escape, $record_end);
}
Expand All @@ -51,15 +54,14 @@ public function feed(string $chunk)
if (strlen($chunk) > 0) {
$chars = str_split($chunk);
foreach ($chars as $char) {
$initial_states = $this->machine->getCurrentStates();
$char_type = $this->getCharType($char);

$this->machine->processInput($char_type);

$this->lastCharType = $char_type;
$end_states = $this->machine->getCurrentStates();

$this->processTransition($initial_states, $end_states, $char);
$this->processTransition($end_states, $char);
}
} else {
throw new \Exception("The CSV parser can not parse empty chunks.");
Expand Down Expand Up @@ -91,7 +93,7 @@ public function getRecords() : array
return $records;
}

public function reset()
public function reset(): void
{
$this->field = "";
$this->fields = [];
Expand All @@ -116,7 +118,7 @@ public function finish()
}
}

private function getCharType($char)
private function getCharType(string $char)
{
$type = sm::CHAR_TYPE_OTHER;
if (in_array($char, $this->recordEnd)) {
Expand All @@ -133,12 +135,12 @@ private function getCharType($char)
return $type;
}

private function addCharToField($char)
private function addCharToField(string $char): void
{
$this->field .= $char;
}

private function createNewRecord()
private function createNewRecord(): void
{
$this->createNewField();
if (!empty($this->fields)) {
Expand All @@ -147,7 +149,7 @@ private function createNewRecord()
}
}

private function createNewField()
private function createNewField(): void
{
if ($this->quoted) {
$this->fields[] = $this->field;
Expand All @@ -159,14 +161,14 @@ private function createNewField()
$this->field = "";
}

private function processTransition($initialStates, $endStates, $input)
private function processTransition($endStates, string $input): void
{
foreach ($endStates as $endState) {
$this->processTransitionHelper($endState, $input);
}
}

private function processTransitionHelper($endState, $input)
private function processTransitionHelper($endState, string $input): void
{
if ($endState == sm::STATE_RECORD_END) {
$this->createNewRecord();
Expand Down
26 changes: 13 additions & 13 deletions src/Parser/StateMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct()
$this->addQuoteFinalTransitions();
}

private function addNewFieldTransitions()
private function addNewFieldTransitions(): void
{
$this->recordEndAndNewFieldCommons(self::STATE_NEW_FIELD);

Expand All @@ -57,7 +57,7 @@ private function addNewFieldTransitions()
);
}

private function noCaptureAndRedundantRecordEndCommon($state)
private function noCaptureAndRedundantRecordEndCommon(string $state): void
{
$this->addTransition(
$state,
Expand Down Expand Up @@ -90,7 +90,7 @@ private function noCaptureAndRedundantRecordEndCommon($state)
);
}

private function addNoCaptureTransitions()
private function addNoCaptureTransitions(): void
{
$this->noCaptureAndRedundantRecordEndCommon(self::STATE_NO_CAPTURE);

Expand All @@ -101,7 +101,7 @@ private function addNoCaptureTransitions()
);
}

private function recordEndAndNewFieldCommons($state)
private function recordEndAndNewFieldCommons(string $state): void
{
$this->addTransition(
$state,
Expand All @@ -128,7 +128,7 @@ private function recordEndAndNewFieldCommons($state)
);
}

private function addRecordEndTransitions()
private function addRecordEndTransitions(): void
{
$this->recordEndAndNewFieldCommons(self::STATE_RECORD_END);

Expand All @@ -139,7 +139,7 @@ private function addRecordEndTransitions()
);
}

private function addRedundantRecordEndTranstions()
private function addRedundantRecordEndTranstions(): void
{
$this->noCaptureAndRedundantRecordEndCommon(self::STATE_REDUNDANT_RECORD_END);

Expand All @@ -150,7 +150,7 @@ private function addRedundantRecordEndTranstions()
);
}

private function addCaptureTransitions()
private function addCaptureTransitions(): void
{
$this->addTransition(
self::STATE_CAPTURE,
Expand Down Expand Up @@ -180,7 +180,7 @@ private function addCaptureTransitions()
);
}

private function addEscapeTransitions()
private function addEscapeTransitions(): void
{
$this->addTransition(
self::STATE_ESCAPE,
Expand All @@ -196,7 +196,7 @@ private function addEscapeTransitions()
);
}

private function addQuoteInitialTransitions()
private function addQuoteInitialTransitions(): void
{
$this->addTransition(
self::STATE_QUOTE_INITIAL,
Expand All @@ -223,7 +223,7 @@ private function addQuoteInitialTransitions()
);
}

private function addQuoteCaptureTransitions()
private function addQuoteCaptureTransitions(): void
{
$this->addTransition(
self::STATE_QUOTE_CAPTURE,
Expand Down Expand Up @@ -255,7 +255,7 @@ private function addQuoteCaptureTransitions()
);
}

private function addQuoteEscapeQuoteTransitions()
private function addQuoteEscapeQuoteTransitions(): void
{
$this->addTransition(
self::STATE_QUOTE_ESCAPE_QUOTE,
Expand All @@ -264,7 +264,7 @@ private function addQuoteEscapeQuoteTransitions()
);
}

private function addQuoteEscapeTransitions()
private function addQuoteEscapeTransitions(): void
{
$this->addTransition(
self::STATE_QUOTE_ESCAPE,
Expand All @@ -280,7 +280,7 @@ private function addQuoteEscapeTransitions()
);
}

private function addQuoteFinalTransitions()
private function addQuoteFinalTransitions(): void
{
$this->addTransition(
self::STATE_QUOTE_FINAL,
Expand Down
Loading

0 comments on commit 1adc0b3

Please sign in to comment.