Skip to content

Commit

Permalink
Merge pull request #17 from bigwhoop/feature/php-82
Browse files Browse the repository at this point in the history
Add PHP 8.2 support, drop PHP 7.4 (EOL)
  • Loading branch information
bigwhoop committed Jul 21, 2023
2 parents 2025a29 + 3e76243 commit dbfefdd
Show file tree
Hide file tree
Showing 34 changed files with 102 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ '7.4', '8.0', '8.1' ]
php-versions: [ '8.0', '8.1', '8.2' ]
composer-deps: [ 'lowest', 'latest' ]
name: PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }} with ${{ matrix.composer-deps }} deps
steps:
Expand Down
12 changes: 11 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@
__DIR__ . '/data',
]);

return (new PhpCsFixer\Config())->setFinder($finder);
$config = new PhpCsFixer\Config();
$config->setFinder($finder);
$config->setRules([
'@Symfony' => true,
'concat_space' => [
'spacing' => 'one',
],
'yoda_style' => false,
]);

return $config;
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ In the end the tokens are re-assembled into the sentences. The user can choose w
- [X] `calculateCurrentTokenProbability` is a big mess. Let's split it up into multiple *Rule* classes. Maybe use a rules engine.
- [ ] Add abbreviations support for different languages.

## Contributing

```
# Check code style
vendor/bin/php-cs-fixer fix --diff --dry-run
# Fix code style
vendor/bin/php-cs-fixer fix --diff
# Run tests
vendor/bin/phpunit
# Run static analysis
vendor/bin/phpstan
```

## Contributors

<a href="https://github.com/bigwhoop/sentence-breaker/graphs/contributors">
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
}
],
"require": {
"php": "^7.4 || ~8.0.0 || ~8.1.0",
"php": "^8.0",
"ext-simplexml": "*"
},
"require-dev": {
"phpunit/phpunit": "^9",
"fabpot/goutte": "^2",
"friendsofphp/php-cs-fixer": "^3.4",
"phpstan/phpstan": "^1.0",
"friendsofphp/php-cs-fixer": "^3.22",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.0"
},
"autoload": {
Expand Down
1 change: 0 additions & 1 deletion data/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@
echo $file . " ... DONE\n";
}


echo "DONE.\n";
5 changes: 2 additions & 3 deletions data/oed-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
declare(strict_types=1);

/**
* Exports data from Oxford English Dictionary
* Exports data from Oxford English Dictionary.
*
* @link http://public.oed.com/how-to-use-the-oed/abbreviations/
* @see http://public.oed.com/how-to-use-the-oed/abbreviations/
*/

require __DIR__ . '/../vendor/autoload.php';

use Goutte\Client;
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
parameters:
phpVersion: 70400 # PHP 7.4
level: 9
paths:
- src
Expand Down
4 changes: 2 additions & 2 deletions src/Abbreviations/FlatFileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class FlatFileProvider implements ValueProvider
private array $fileNames;

/**
* @param string $basePath
* @param string[] $fileNames
*/
public function __construct(string $basePath, array $fileNames)
Expand All @@ -25,6 +24,7 @@ public function __construct(string $basePath, array $fileNames)

/**
* @return array<string>
*
* @throws Exception
*/
public function getValues(): array
Expand All @@ -50,7 +50,7 @@ public function getValues(): array
*/
private function getPaths(): array
{
$pattern = $this->basePath.'/{'.implode(',', $this->fileNames).'}.txt';
$pattern = $this->basePath . '/{' . implode(',', $this->fileNames) . '}.txt';
$paths = glob($pattern, GLOB_BRACE);
if ($paths === false) {
throw new Exception(sprintf('Unable to find files with pattern "%s"', $pattern));
Expand Down
20 changes: 11 additions & 9 deletions src/GeneratorOffsetIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,36 @@

namespace Bigwhoop\SentenceBreaker;

use Generator;
use Iterator;

/**
* @template T
*
* @implements Iterator<T>
*/
class GeneratorOffsetIterator implements Iterator
class GeneratorOffsetIterator implements \Iterator
{
/**
* @var Generator<int, T, mixed, void>
*/
private Generator $generator;
/** @var \Generator<int, T, mixed, void> */
private \Generator $generator;

/** @var array<T> */
private array $cache = [];

private int $currentIndex = 0;

/**
* @param Generator<int, T, mixed, void> $generator
* @param \Generator<int, T, mixed, void> $generator
*/
public function __construct(iterable $generator)
public function __construct(\Generator $generator)
{
$this->generator = $generator;
$this->addCurrentToCache();
}

/**
* @return T
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->cache[$this->currentIndex];
Expand All @@ -44,7 +46,7 @@ public function next(): void
$this->addCurrentToCache();
}

$this->currentIndex++;
++$this->currentIndex;
}

public function key(): int
Expand Down
4 changes: 2 additions & 2 deletions src/Lexing/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Lexer

private ?States\State $state;

/** @var int The current position in $input */
/** @var int The current position in */
private int $pos = 0;

/** @var int The start position of the current token */
Expand All @@ -28,8 +28,8 @@ public function __construct()
}

/**
* @param string $input
* @return iterable<Token>
*
* @throws States\StateException
*/
public function run(string $input): iterable
Expand Down
2 changes: 1 addition & 1 deletion src/Lexing/States/ParenthesesState.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function call(Lexer $lexer): ?State
$next = $lexer->next();

if ($next === null) {
throw new StateException('Failed to find closing parentheses. Reached end of input. Read: '.$lexer->getTokenValue());
throw new StateException('Failed to find closing parentheses. Reached end of input. Read: ' . $lexer->getTokenValue());
}

if ($next === $closing) {
Expand Down
4 changes: 0 additions & 4 deletions src/Lexing/States/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
abstract class State
{
/**
* @param Lexer $lexer
* @return State|null
* @throws StateException
*/
final public function __invoke(Lexer $lexer): ?State
Expand All @@ -19,8 +17,6 @@ final public function __invoke(Lexer $lexer): ?State
}

/**
* @param Lexer $lexer
* @return State|null
* @throws StateException
*/
abstract protected function call(Lexer $lexer): ?State;
Expand Down
3 changes: 0 additions & 3 deletions src/Lexing/States/TextState.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

class TextState extends State
{
/**
* {@inheritdoc}
*/
protected function call(Lexer $lexer): ?State
{
while (true) {
Expand Down
3 changes: 0 additions & 3 deletions src/Lexing/States/WhitespaceState.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class WhitespaceState extends State
{
public const CHARS = [' ', "\t", "\r", "\n"];

/**
* {@inheritdoc}
*/
protected function call(Lexer $lexer): ?State
{
while (in_array($lexer->peek(), self::CHARS, true)) {
Expand Down
3 changes: 0 additions & 3 deletions src/Lexing/States/WordState.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ private function getNonWordChars(): array
return array_merge(['.', '?', '!', null], WhitespaceState::CHARS);
}

/**
* {@inheritdoc}
*/
protected function call(Lexer $lexer): ?State
{
$nonWordChars = $this->getNonWordChars();
Expand Down
3 changes: 0 additions & 3 deletions src/Lexing/Tokens/PeriodToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public function getName(): string
return 'T_PERIOD';
}

/**
* {@inheritdoc}
*/
public function getPrintableValue(): string
{
return '.';
Expand Down
1 change: 0 additions & 1 deletion src/Lexing/Tokens/ValueToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

abstract class ValueToken implements Token
{
/** @var string */
private string $value;

public function __construct(string $value = '')
Expand Down
9 changes: 5 additions & 4 deletions src/ProbabilityCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use Bigwhoop\SentenceBreaker\Abbreviations\Abbreviations;
use Bigwhoop\SentenceBreaker\Lexing\Tokens\AbbreviationToken;
use Bigwhoop\SentenceBreaker\Lexing\Tokens\PotentialAbbreviationToken;
use Bigwhoop\SentenceBreaker\Lexing\Tokens\Token;
use Bigwhoop\SentenceBreaker\Rules\ConfigurationException;
use Bigwhoop\SentenceBreaker\Rules\Rules;
use Bigwhoop\SentenceBreaker\Lexing\Tokens\Token;
use Generator;

class ProbabilityCalculator
{
Expand Down Expand Up @@ -46,10 +45,12 @@ public function addRules(Rules $rules): void

/**
* @param iterable<Token> $tokens
* @return Generator<TokenProbability>
*
* @return \Generator<TokenProbability>
*
* @throws ConfigurationException
*/
public function calculate(iterable $tokens): Generator
public function calculate(iterable $tokens): \Generator
{
$tokenGenerator = function () use ($tokens) {
foreach ($tokens as $token) {
Expand Down
1 change: 0 additions & 1 deletion src/Rules/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
interface Configuration
{
/**
* @return Rules
* @throws ConfigurationException
*/
public function getRules(): Rules;
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/IniConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class IniConfiguration implements Configuration
private array $data = [];

/**
* @return self
* @throws ConfigurationException
*/
public static function loadFile(string $path): self
Expand All @@ -30,7 +29,7 @@ public static function loadFile(string $path): self
}

/**
@throws ConfigurationException
* @throws ConfigurationException
*/
public function __construct(string $data)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Rules/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

class Rule
{
/** @var string */
private string $tokenName;

/** @var RulePattern[] */
private array $patterns = [];

/**
* @param string $tokenName
* @param RulePattern[] $patterns
*/
public function __construct(string $tokenName, array $patterns = [])
Expand Down
7 changes: 2 additions & 5 deletions src/Rules/RulePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

class RulePattern
{
/** @var int */
private int $probability;

/** @var array<int, RulePatternToken> */
private array $tokens = [];

/**
* @param int $probability
* @param array<int, RulePatternToken> $tokens
*/
public function __construct(int $probability, array $tokens = [])
Expand Down Expand Up @@ -66,15 +64,14 @@ public function getTokensOffsetRelativeToStartToken(string $startTokenName): arr
}

if ($startTokenIdx === null) {
throw new ConfigurationException('No start token found for pattern '.print_r($this, true));
throw new ConfigurationException('No start token found for pattern ' . print_r($this, true));
}

$offsets = array_map(
static fn (int $idx) => $idx - $startTokenIdx,
range(0, count($this->tokens) -1)
range(0, count($this->tokens) - 1)
);

/** @phpstan-ignore-next-line */
return array_combine($offsets, $this->tokens);
}
}
6 changes: 0 additions & 6 deletions src/Rules/RulePatternToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@

class RulePatternToken
{
/** @var string */
private string $tokenName;

/** @var bool */
private bool $isStartToken;

/**
* @param string $tokenName
* @param bool $isStartToken
*/
public function __construct(string $tokenName, bool $isStartToken = false)
{
$this->tokenName = $tokenName;
Expand Down
2 changes: 0 additions & 2 deletions src/Rules/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function hasRule(string $tokenName): bool
}

/**
* @param string $tokenName
* @return Rule
* @throws ConfigurationException
*/
public function getRule(string $tokenName): Rule
Expand Down
Loading

0 comments on commit dbfefdd

Please sign in to comment.