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

Upgrade to Symfony 4 #3

Open
wants to merge 1 commit 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
45 changes: 25 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
language: php

sudo: false

php:
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- 7.2
- 7.3
- 7.4

cache:
directories:
- $HOME/.composer/cache

env:
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION=2.7.*
- SYMFONY_VERSION=2.8.*
- SYMFONY_VERSION=3.0.*
- SYMFONY_VERSION=3.1.*
# - SYMFONY_VERSION=dev-master
matrix:
exclude:
- php: '5.4'
env: SYMFONY_VERSION=3.0.*
- php: '5.4'
env: SYMFONY_VERSION=3.1.*
# - php: '5.4'
# env: SYMFONY_VERSION=dev-master
matrix:
- SYMFONY_VERSION=3.4.*
- SYMFONY_VERSION=4.4.*

before_install:
- travis_retry composer self-update
- composer require "symfony/framework-bundle:${SYMFONY_VERSION}" --no-update
- composer require "symfony/property-access:${SYMFONY_VERSION}" --dev --no-update
install: composer install
script: bin/phpunit

install:
- travis_retry composer require --no-update symfony/framework-bundle:${SYMFONY_VERSION}
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction

script:
- "composer test"
- "composer psalm"
- "phpunit --testsuite=unit --coverage-text --coverage-clover build/logs/clover.xml"
18 changes: 10 additions & 8 deletions Configuration/AbstractConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*/
abstract class AbstractConfiguration
{
const LINE_ENDING_CRLF = "\r\n";
const LINE_ENDING_LF = "\n";
const LINE_ENDING_CR = "\r";
public const LINE_ENDING_CRLF = "\r\n";
public const LINE_ENDING_LF = "\n";
public const LINE_ENDING_CR = "\r";

/**
* Delimiting character
Expand All @@ -31,7 +31,7 @@ abstract class AbstractConfiguration
*
* @return string
*/
public function getDelimiter()
public function getDelimiter(): string
{
return $this->delimiter;
}
Expand All @@ -43,11 +43,12 @@ public function getDelimiter()
*
* @throws InvalidConfigurationException
*/
public function setDelimiter($delimiter)
public function setDelimiter($delimiter): void
{
if (strlen($delimiter) != 1) {
if (strlen($delimiter) !== 1) {
throw new InvalidConfigurationException('CSV Configuration error: Delimiter must be exactly 1 character');
}

$this->delimiter = $delimiter;
}

Expand All @@ -68,11 +69,12 @@ public function getLineEnding()
*
* @throws InvalidConfigurationException
*/
public function setLineEnding($lineEnding)
public function setLineEnding($lineEnding): void
{
if (!in_array($lineEnding, array(self::LINE_ENDING_CR, self::LINE_ENDING_CRLF, self::LINE_ENDING_LF))) {
if (!in_array($lineEnding, array(self::LINE_ENDING_CR, self::LINE_ENDING_CRLF, self::LINE_ENDING_LF), true)) {
throw new InvalidConfigurationException('Invalid line ending provided. Only CR,LF and CRLF allowed');
}

$this->lineEnding = $lineEnding;
}
}
12 changes: 9 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('kuborgh_csv');
$treeBuilder = new TreeBuilder('kuborgh_csv');
if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
/** @psalm-suppress UndefinedMethod */
/** @psalm-suppress DeprecatedMethod */
$rootNode = $treeBuilder->root('kuborgh_csv');
}

$rootNode
->children()
Expand Down
9 changes: 6 additions & 3 deletions DependencyInjection/KuborghCsvExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Kuborgh\CsvBundle\DependencyInjection;

use Exception;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -23,6 +24,8 @@ class KuborghCsvExtension extends Extension
{
/**
* {@inheritDoc}
*
* @throws Exception
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand All @@ -43,7 +46,7 @@ public function load(array $configs, ContainerBuilder $container)
* @param array $config Config
* @param ContainerBuilder $container Container
*/
protected function loadParserConfig(array $config, ContainerBuilder $container)
protected function loadParserConfig(array $config, ContainerBuilder $container): void
{
foreach ($config as $parserName => $parserConfig) {
$parserConfigClass = $container->getParameter('kuborgh_csv.configuration.parser.class');
Expand All @@ -66,7 +69,7 @@ protected function loadParserConfig(array $config, ContainerBuilder $container)
* @param array $config Config
* @param ContainerBuilder $container Container
*/
protected function loadGeneratorConfig(array $config, ContainerBuilder $container)
protected function loadGeneratorConfig(array $config, ContainerBuilder $container): void
{
foreach ($config as $parserName => $parserConfig) {
// Prepare config object with common settings
Expand All @@ -92,7 +95,7 @@ protected function loadGeneratorConfig(array $config, ContainerBuilder $containe
*
* @return Definition
*/
protected function loadCommonConfig($parserConfig, $parserConfigClass)
protected function loadCommonConfig($parserConfig, $parserConfigClass): Definition
{
$parserConfigDef = new Definition($parserConfigClass);

Expand Down
2 changes: 1 addition & 1 deletion Generator/GeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public function __construct(GeneratorConfiguration $configuration);
*
* @return string CSV
*/
public function generate(array $array);
public function generate(array $array): string;
}
10 changes: 6 additions & 4 deletions Generator/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Kuborgh\CsvBundle\Generator;

use InvalidArgumentException;

/**
* Generates CSV by means of native php function fputcsv()
* This may not be conform to rfc4180.
* Also the line ending is for example not configurable
*/
class PhpGenerator extends AbstractGenerator implements GeneratorInterface
class PhpGenerator extends AbstractGenerator
{
/**
* Generate csv string from array
Expand All @@ -16,14 +18,14 @@ class PhpGenerator extends AbstractGenerator implements GeneratorInterface
*
* @return string CSV
*/
public function generate(array $array)
public function generate(array $array): string
{
$buffer = fopen('php://temp', 'r+');
$buffer = fopen('php://temp', 'rb+');
$delim = $this->configuration->getDelimiter();

foreach ($array as $row) {
if (!is_array($row)) {
throw new \InvalidArgumentException('Expecting a 2-dimensional array as value');
throw new InvalidArgumentException('Expecting a 2-dimensional array as value');
}
fputcsv($buffer, $row, $delim);
}
Expand Down
7 changes: 3 additions & 4 deletions Generator/StringGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Generates rfc4180 conform csv by concatenating strings
*/
class StringGenerator extends AbstractGenerator implements GeneratorInterface
class StringGenerator extends AbstractGenerator
{
/**
* Generate csv string from array
Expand All @@ -14,7 +14,7 @@ class StringGenerator extends AbstractGenerator implements GeneratorInterface
*
* @return string CSV
*/
public function generate(array $array)
public function generate(array $array): string
{
$delim = $this->configuration->getDelimiter();
$lineEnd = $this->configuration->getLineEnding();
Expand Down Expand Up @@ -52,8 +52,7 @@ public function generate(array $array)
}
$lines[] = implode($delim, $newRow);
}
$string = implode($lineEnd, $lines);

return $string;
return implode($lineEnd, $lines);
}
}
30 changes: 16 additions & 14 deletions Parser/CharacterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Full-featured rfc4180 parser, but may be slow for large files.
* The csv is parsed bytewise with lookahead
*/
class CharacterParser extends AbstractParser implements ParserInterface
class CharacterParser extends AbstractParser
{
/**
* Pointer to the next character being read
Expand Down Expand Up @@ -59,7 +59,7 @@ class CharacterParser extends AbstractParser implements ParserInterface
*
* @return array
*/
public function parse($csvString)
public function parse($csvString): array
{
$this->init($csvString);

Expand Down Expand Up @@ -88,7 +88,7 @@ public function parse($csvString)
case $lineBreak1:
// Do we need an extra character?
if ($lineBreak2) {
if ($this->preview() == $lineBreak2) {
if ($this->preview() === $lineBreak2) {
$this->read();
$this->finishField();
$this->finishRow();
Expand Down Expand Up @@ -119,7 +119,7 @@ public function parse($csvString)
*
* @param string $csvString
*/
protected function init($csvString)
protected function init(string $csvString): void
{
$this->nextChar = 0;
$this->csvString = $csvString;
Expand All @@ -130,9 +130,10 @@ protected function init($csvString)
* Read the current character and forward the pointer
*
* @return string one character
*
* @throws EofException
*/
protected function read()
protected function read(): string
{
$char = $this->preview();
$this->nextChar++;
Expand All @@ -144,29 +145,30 @@ protected function read()
* Peak into the next character without forwarding the pointer
*
* @return string one character
*
* @throws EofException
*/
protected function preview()
protected function preview(): string
{
if ($this->nextChar >= $this->csvLength) {
throw new EofException('EOF reached');
}
$char = $this->csvString[$this->nextChar];

return $char;
return $this->csvString[$this->nextChar];
}

/**
* Reads complete string until next quotation mark.
* NOTE: The pointer is increased more than it is read (to simulate the quote has been read, too)
*
* @return string
*
* @throws EofException
*/
protected function leap()
protected function leap(): string
{
$pos = strpos($this->csvString, '"', $this->nextChar);
if ($pos === false) {
if (false === $pos) {
throw new EofException('EOF reached (in quoted string)');
}
$chars = substr($this->csvString, $this->nextChar, $pos - $this->nextChar);
Expand All @@ -178,15 +180,15 @@ protected function leap()
/**
* Read quoted string until quote end is reached.
*/
protected function readQuotedString()
protected function readQuotedString(): void
{
do {
// Leap forward to next quotation mark
$this->field .= $this->leap();

try {
// Two quotes are one quote in the string
if ($this->preview() == '"') {
if ('"' === $this->preview()) {
$this->field .= $this->read();
} else {
// Quote ended
Expand All @@ -203,7 +205,7 @@ protected function readQuotedString()
*
* @param bool $force When force is true, even empty fields are added
*/
protected function finishField($force = true)
protected function finishField($force = true): void
{
if ($force || !empty($this->field)) {
$this->row[] = $this->field;
Expand All @@ -214,7 +216,7 @@ protected function finishField($force = true)
/**
* Row is complete and can be added to result
*/
protected function finishRow()
protected function finishRow(): void
{
$this->rows[] = $this->row;
$this->row = array();
Expand Down
2 changes: 1 addition & 1 deletion Parser/ParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public function __construct(ParserConfiguration $configuration);
*
* @return array
*/
public function parse($csvString);
public function parse($csvString): array;
}
4 changes: 2 additions & 2 deletions Parser/SimpleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Very very simple CSV Parser without escaping support
*/
class SimpleParser extends AbstractParser implements ParserInterface
class SimpleParser extends AbstractParser
{
/**
* Parse the given string into an php array
Expand All @@ -14,7 +14,7 @@ class SimpleParser extends AbstractParser implements ParserInterface
*
* @return array
*/
public function parse($csvString)
public function parse($csvString): array
{
$lineEnding = $this->configuration->getLineEnding();
$delimiter = $this->configuration->getDelimiter();
Expand Down
Loading