Skip to content

Commit

Permalink
Updated to PHP 7.4
Browse files Browse the repository at this point in the history
- Removed dependency on tale-dev-tool
- Added property types
- Some minor fixes
  • Loading branch information
TorbenKoehn committed Mar 11, 2020
1 parent 47a37b7 commit e2570f3
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 78 deletions.
16 changes: 3 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@

language: php

git:
depth: 5

php:
- 7.1
- 7.2
- 7.3

- 7.4
install:
- travis_retry composer clear-cache
- travis_retry composer self-update
- travis_retry composer install

script:
- vendor/bin/tale-dev check --report --coverage-php-version=7.1

addons:
code_climate:
repo_token: 9c681c931a1a0411dfc5f0ed09cb647976fc142692b5141f444a336c1dadc2ec
- composer lint
- composer test
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@
"description": "A reading library that can read and parse strings and PSR-7 streams",
"license": "MIT",
"homepage": "http://docs.talesoft.codes/php/tale/reader",
"scripts": {
"test": "phpunit",
"test:coverage-html": "phpunit --coverage-html=coverage",
"lint": "phpcs",
"lint:fix": "phpcbf"
},
"authors": [
{
"name": "Torben Koehn",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.1.0",
"php": ">=7.4.0",
"ext-ctype": "*",
"psr/http-message": "^1.0"
},
"require-dev": {
"talesoft/tale-dev-tool": "dev-master",
"phpunit/phpunit": "^8.4",
"squizlabs/php_codesniffer": "^3.5",
"talesoft/tale-stream": "dev-master"
},
"autoload": {
Expand Down
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset name="PSR-2">
<rule ref="PSR2"/>
<file>./src</file>
<file>./tests</file>
</ruleset>
21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.4/phpunit.xsd"
colors="true"
bootstrap="./vendor/autoload.php"
forceCoversAnnotation="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true">
<testsuites>
<testsuite name="Talesoft - Tale Reader">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
32 changes: 9 additions & 23 deletions src/Reader/StreamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,10 @@

final class StreamReader implements ReaderInterface
{
/**
* @var StreamInterface
*/
private $stream;

/**
* @var string
*/
private $buffer = '';

/**
* @var int
*/
private $bufferSize;

/**
* @var int
*/
private $nextConsumeLength = 0;
private StreamInterface $stream;
private string $buffer = '';
private int $bufferSize;
private int $nextConsumeLength = 0;

/**
* creates a new streamReader instance.
Expand Down Expand Up @@ -81,8 +66,10 @@ public function consume(int $length = 0): string
return '';
}
$this->expandBuffer($length);
$length = min(strlen($this->buffer), $length);
$consumedBytes = substr($this->buffer, 0, $length);
$this->buffer = substr($this->buffer, $length);
$newBuffer = substr($this->buffer, $length);
$this->buffer = $newBuffer !== false ? $newBuffer : '';
$this->nextConsumeLength = 0;
return $consumedBytes;
}
Expand All @@ -94,9 +81,8 @@ public function consume(int $length = 0): string
*/
private function expandBuffer(int $length): void
{
if (\strlen($this->buffer) >= $length || $this->stream->eof()) {
return;
while (\strlen($this->buffer) < $length && !$this->stream->eof()) {
$this->buffer .= $this->stream->read($this->bufferSize);
}
$this->buffer .= $this->stream->read($this->bufferSize);
}
}
11 changes: 2 additions & 9 deletions src/Reader/StringReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

final class StringReader implements ReaderInterface
{
/**
* @var string
*/
private $string;

/**
* @var int
*/
private $nextConsumeLength = 0;
private string $string;
private int $nextConsumeLength = 0;

public function __construct(string $string)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Text/Expression/NumberExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

final class NumberExpression
{
private $integerPart;
private $decimalPart;
private string $integerPart;
private string $decimalPart;

/**
* NumberValue constructor.
Expand Down
8 changes: 3 additions & 5 deletions src/Reader/Text/ExpressionReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ final class ExpressionReader
{
public const DEFAULT_ESCAPE_TEXT = '\\';

/** @var TextReader */
private $textReader;
private TextReader $textReader;

public function __construct(TextReader $textReader)
{
Expand Down Expand Up @@ -96,9 +95,8 @@ public function readNumber(?string $decimalDelimiter = '.', ?string $thousandsDe
return null;
}

$isDigit = function (string $byte) use ($thousandsDelimiter) {
return ctype_digit($byte) || ($thousandsDelimiter !== null && $byte === $thousandsDelimiter);
};
$isDigit = fn (string $byte) =>
ctype_digit($byte) || ($thousandsDelimiter !== null && $byte === $thousandsDelimiter);

$integer = $this->textReader->readWhile($isDigit) ?: '0';
$decimal = '0';
Expand Down
10 changes: 2 additions & 8 deletions src/Reader/Text/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@

final class Location
{
/**
* @var int
*/
private $line;
/**
* @var int
*/
private $offset;
private int $line;
private int $offset;

/**
* TextLocation constructor.
Expand Down
5 changes: 1 addition & 4 deletions src/Reader/Text/ReadException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

final class ReadException extends \RuntimeException
{
/**
* @var Location
*/
private $location;
private Location $location;

public function __construct(Location $location, string $message = '', int $code = 0, Throwable $previous = null)
{
Expand Down
21 changes: 10 additions & 11 deletions src/Reader/TextReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ final class TextReader implements ReaderInterface
public const LINE_DELIMITER_CRLF = "\r\n";
public const LINE_DELIMITER_SYSTEM = \PHP_EOL;

/** @var ReaderInterface */
private $reader;
private $currentLine = 0;
private $currentOffset = 0;
private $lineDelimiter;
private ReaderInterface $reader;
private int $currentLine = 0;
private int $currentOffset = 0;
private string $lineDelimiter;

public function __construct(ReaderInterface $reader, string $lineDelimiter = self::LINE_DELIMITER_LF)
{
Expand Down Expand Up @@ -101,9 +100,7 @@ public function readWhile(callable $callback, int $peekLength = 1, bool $inclusi

public function readUntil(callable $callback, int $peekLength = 1, bool $inclusive = false): string
{
return $this->readWhile(function (string $bytes) use ($callback) {
return !$callback($bytes);
}, $peekLength, $inclusive);
return $this->readWhile(fn (string $bytes) => !$callback($bytes), $peekLength, $inclusive);
}

public function peekText(string $text): bool
Expand All @@ -118,9 +115,11 @@ public function peekNewLine(): bool

public function readLine(): string
{
return trim($this->readUntil(function (string $bytes) {
return $bytes === $this->lineDelimiter;
}, \strlen($this->lineDelimiter), true), self::LINE_DELIMITER_CRLF);
return trim($this->readUntil(
fn (string $bytes) => $bytes === $this->lineDelimiter,
\strlen($this->lineDelimiter),
true,
), self::LINE_DELIMITER_CRLF);
}

public function peekSpace(): bool
Expand Down
3 changes: 2 additions & 1 deletion tests/Reader/Text/ExpressionReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Tale\Reader\StreamReader;
use Tale\Reader\Text\Expression\NumberExpression;
use Tale\Reader\Text\ExpressionReader;
use Tale\Reader\Text\ReadException;
use Tale\Reader\TextReader;
use function Tale\stream_memory;

Expand All @@ -31,10 +32,10 @@ public function testReadContainedText(): void
/**
* @covers ::__construct
* @covers ::readContainedText
* @expectedException \Tale\Reader\Text\ReadException
*/
public function testReadContainedTextThrowsExceptionOnMissingCloseText(): void
{
$this->expectException(ReadException::class);
$stream = new StreamReader(stream_memory('STARTTEXTSOMEESCAPEENDTEXTACTUALENDTEXDEMACIA'));
$reader = new ExpressionReader($textReader = new TextReader($stream));
$reader->readContainedText('STARTTEXT', 'ENDTEXT', 'ESCAPE');
Expand Down

0 comments on commit e2570f3

Please sign in to comment.