From f12ad118d50d6bac2f7fe3423997ed7f30ee368f Mon Sep 17 00:00:00 2001 From: Philippe Gerber Date: Wed, 26 Jan 2022 10:16:44 +0100 Subject: [PATCH 1/2] updated code style to Symfony 2 --- .php-cs-fixer.php | 12 ++++++++- README.md | 16 ++++++++++++ data/filter.php | 1 - data/oed-export.php | 5 ++-- src/Abbreviations/FlatFileProvider.php | 4 +-- src/GeneratorOffsetIterator.php | 2 +- src/Lexing/Lexer.php | 4 +-- src/Lexing/States/ParenthesesState.php | 2 +- src/Lexing/States/State.php | 4 --- src/Lexing/Tokens/ValueToken.php | 1 - src/ProbabilityCalculator.php | 4 ++- src/Rules/Configuration.php | 1 - src/Rules/IniConfiguration.php | 1 - src/Rules/Rule.php | 2 -- src/Rules/RulePattern.php | 8 +++--- src/Rules/RulePatternToken.php | 6 ----- src/Rules/Rules.php | 2 -- src/Rules/XMLConfiguration.php | 4 +-- src/SentenceBreaker.php | 15 +++++------ .../Abbreviations/FlatFileProviderTest.php | 8 +++--- tests/src/GeneratorOffsetIteratorTest.php | 2 +- tests/src/ProbabilityCalculatorTest.php | 26 ++++++++----------- tests/src/Rules/IniConfigurationTest.php | 4 +-- tests/src/Rules/XMLConfigurationTest.php | 2 +- tests/src/SentenceBreakerPerformanceTest.php | 4 +-- tests/src/SentenceBreakerTest.php | 21 ++++++++------- 26 files changed, 80 insertions(+), 81 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index aa79597..a80ad8f 100755 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -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; \ No newline at end of file diff --git a/README.md b/README.md index 25c855d..4362bcd 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/data/filter.php b/data/filter.php index 58614e0..7ad3b66 100755 --- a/data/filter.php +++ b/data/filter.php @@ -28,5 +28,4 @@ echo $file . " ... DONE\n"; } - echo "DONE.\n"; diff --git a/data/oed-export.php b/data/oed-export.php index 86120b3..bfe9de1 100755 --- a/data/oed-export.php +++ b/data/oed-export.php @@ -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; diff --git a/src/Abbreviations/FlatFileProvider.php b/src/Abbreviations/FlatFileProvider.php index 6e39f63..4319e69 100755 --- a/src/Abbreviations/FlatFileProvider.php +++ b/src/Abbreviations/FlatFileProvider.php @@ -14,7 +14,6 @@ class FlatFileProvider implements ValueProvider private array $fileNames; /** - * @param string $basePath * @param string[] $fileNames */ public function __construct(string $basePath, array $fileNames) @@ -25,6 +24,7 @@ public function __construct(string $basePath, array $fileNames) /** * @return array + * * @throws Exception */ public function getValues(): array @@ -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)); diff --git a/src/GeneratorOffsetIterator.php b/src/GeneratorOffsetIterator.php index 6c5aeb8..18d9ce1 100644 --- a/src/GeneratorOffsetIterator.php +++ b/src/GeneratorOffsetIterator.php @@ -44,7 +44,7 @@ public function next(): void $this->addCurrentToCache(); } - $this->currentIndex++; + ++$this->currentIndex; } public function key(): int diff --git a/src/Lexing/Lexer.php b/src/Lexing/Lexer.php index 56325aa..a0b90d1 100755 --- a/src/Lexing/Lexer.php +++ b/src/Lexing/Lexer.php @@ -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 */ @@ -28,8 +28,8 @@ public function __construct() } /** - * @param string $input * @return iterable + * * @throws States\StateException */ public function run(string $input): iterable diff --git a/src/Lexing/States/ParenthesesState.php b/src/Lexing/States/ParenthesesState.php index 64f2b30..7fd57b3 100755 --- a/src/Lexing/States/ParenthesesState.php +++ b/src/Lexing/States/ParenthesesState.php @@ -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) { diff --git a/src/Lexing/States/State.php b/src/Lexing/States/State.php index 0175f51..8639426 100755 --- a/src/Lexing/States/State.php +++ b/src/Lexing/States/State.php @@ -9,8 +9,6 @@ abstract class State { /** - * @param Lexer $lexer - * @return State|null * @throws StateException */ final public function __invoke(Lexer $lexer): ?State @@ -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; diff --git a/src/Lexing/Tokens/ValueToken.php b/src/Lexing/Tokens/ValueToken.php index 26ccd56..3fc2b2f 100755 --- a/src/Lexing/Tokens/ValueToken.php +++ b/src/Lexing/Tokens/ValueToken.php @@ -6,7 +6,6 @@ abstract class ValueToken implements Token { - /** @var string */ private string $value; public function __construct(string $value = '') diff --git a/src/ProbabilityCalculator.php b/src/ProbabilityCalculator.php index 61e6f3c..7fd3467 100755 --- a/src/ProbabilityCalculator.php +++ b/src/ProbabilityCalculator.php @@ -7,9 +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 @@ -46,7 +46,9 @@ public function addRules(Rules $rules): void /** * @param iterable $tokens + * * @return Generator + * * @throws ConfigurationException */ public function calculate(iterable $tokens): Generator diff --git a/src/Rules/Configuration.php b/src/Rules/Configuration.php index 4b1da77..061381d 100755 --- a/src/Rules/Configuration.php +++ b/src/Rules/Configuration.php @@ -7,7 +7,6 @@ interface Configuration { /** - * @return Rules * @throws ConfigurationException */ public function getRules(): Rules; diff --git a/src/Rules/IniConfiguration.php b/src/Rules/IniConfiguration.php index 40faa5b..d7567f8 100755 --- a/src/Rules/IniConfiguration.php +++ b/src/Rules/IniConfiguration.php @@ -12,7 +12,6 @@ class IniConfiguration implements Configuration private array $data = []; /** - * @return self * @throws ConfigurationException */ public static function loadFile(string $path): self diff --git a/src/Rules/Rule.php b/src/Rules/Rule.php index 368ba92..af08632 100755 --- a/src/Rules/Rule.php +++ b/src/Rules/Rule.php @@ -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 = []) diff --git a/src/Rules/RulePattern.php b/src/Rules/RulePattern.php index 0e000c0..3449f4d 100755 --- a/src/Rules/RulePattern.php +++ b/src/Rules/RulePattern.php @@ -6,14 +6,12 @@ class RulePattern { - /** @var int */ private int $probability; /** @var array */ private array $tokens = []; /** - * @param int $probability * @param array $tokens */ public function __construct(int $probability, array $tokens = []) @@ -66,15 +64,15 @@ 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 */ + /* @phpstan-ignore-next-line */ return array_combine($offsets, $this->tokens); } } diff --git a/src/Rules/RulePatternToken.php b/src/Rules/RulePatternToken.php index 251e548..7131adb 100755 --- a/src/Rules/RulePatternToken.php +++ b/src/Rules/RulePatternToken.php @@ -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; diff --git a/src/Rules/Rules.php b/src/Rules/Rules.php index f54237e..81e7d36 100755 --- a/src/Rules/Rules.php +++ b/src/Rules/Rules.php @@ -50,8 +50,6 @@ public function hasRule(string $tokenName): bool } /** - * @param string $tokenName - * @return Rule * @throws ConfigurationException */ public function getRule(string $tokenName): Rule diff --git a/src/Rules/XMLConfiguration.php b/src/Rules/XMLConfiguration.php index 90b0921..98b3012 100755 --- a/src/Rules/XMLConfiguration.php +++ b/src/Rules/XMLConfiguration.php @@ -11,8 +11,6 @@ class XMLConfiguration implements Configuration private SimpleXMLElement $data; /** - * @param string $path - * @return self * @throws ConfigurationException */ public static function loadFile(string $path): self @@ -106,7 +104,7 @@ public function getRules(): Rules $rule->addPattern($pattern); - $patternIdx++; + ++$patternIdx; } $rules->addRule($rule); diff --git a/src/SentenceBreaker.php b/src/SentenceBreaker.php index 71e74c9..6aea620 100755 --- a/src/SentenceBreaker.php +++ b/src/SentenceBreaker.php @@ -5,13 +5,13 @@ namespace Bigwhoop\SentenceBreaker; use Bigwhoop\SentenceBreaker\Abbreviations\Abbreviations; +use Bigwhoop\SentenceBreaker\Abbreviations\ArrayProvider; +use Bigwhoop\SentenceBreaker\Abbreviations\ValueProvider; use Bigwhoop\SentenceBreaker\Exceptions\InvalidArgumentException; +use Bigwhoop\SentenceBreaker\Lexing\Lexer; use Bigwhoop\SentenceBreaker\Rules\Configuration; use Bigwhoop\SentenceBreaker\Rules\ConfigurationException; use Bigwhoop\SentenceBreaker\Rules\IniConfiguration; -use Bigwhoop\SentenceBreaker\Abbreviations\ArrayProvider; -use Bigwhoop\SentenceBreaker\Abbreviations\ValueProvider; -use Bigwhoop\SentenceBreaker\Lexing\Lexer; use Bigwhoop\SentenceBreaker\Rules\Rules; use Generator; @@ -20,17 +20,13 @@ class SentenceBreaker /** @var ValueProvider[] */ private array $abbreviationProviders = []; - /** @var Lexer */ private Lexer $lexer; - /** @var ProbabilityCalculator */ private ProbabilityCalculator $probabilityCalculator; - /** @var SentenceBuilder */ private SentenceBuilder $sentenceBuilder; /** - * @param Configuration|null $rulesConfig * @throws ConfigurationException */ public function __construct(?Configuration $rulesConfig = null) @@ -47,7 +43,7 @@ public function __construct(?Configuration $rulesConfig = null) */ private function loadDefaultRules(): Rules { - return IniConfiguration::loadFile(__DIR__.'/../rules/rules.ini')->getRules(); + return IniConfiguration::loadFile(__DIR__ . '/../rules/rules.ini')->getRules(); } public function setLexer(Lexer $lexer): void @@ -85,7 +81,7 @@ public function addAbbreviations($values): void if (is_array($values)) { $values = new ArrayProvider($values); } elseif (!($values instanceof ValueProvider)) { - throw new InvalidArgumentException('Values argument must either be an array or an instance of '.ValueProvider::class); + throw new InvalidArgumentException('Values argument must either be an array or an instance of ' . ValueProvider::class); } $this->abbreviationProviders[] = $values; @@ -93,6 +89,7 @@ public function addAbbreviations($values): void /** * @return Generator + * * @throws ConfigurationException * @throws Lexing\States\StateException */ diff --git a/tests/src/Abbreviations/FlatFileProviderTest.php b/tests/src/Abbreviations/FlatFileProviderTest.php index e8d1392..914e963 100755 --- a/tests/src/Abbreviations/FlatFileProviderTest.php +++ b/tests/src/Abbreviations/FlatFileProviderTest.php @@ -11,22 +11,22 @@ class FlatFileProviderTest extends TestCase { public function testSingleFile(): void { - $provider = new FlatFileProvider(__DIR__.'/../../assets/data', ['abbr1']); + $provider = new FlatFileProvider(__DIR__ . '/../../assets/data', ['abbr1']); $this->assertEquals(['Dr', 'Prof'], $provider->getValues()); } public function testMultipleFiles(): void { - $provider = new FlatFileProvider(__DIR__.'/../../assets/data', ['abbr1', 'abbr2']); + $provider = new FlatFileProvider(__DIR__ . '/../../assets/data', ['abbr1', 'abbr2']); $this->assertEquals(['Dr', 'Mr.', 'Mrs.', 'Ms.', 'Prof'], $provider->getValues()); } public function testPatternMatching(): void { - $provider = new FlatFileProvider(__DIR__.'/../../assets/data', ['abbr*']); + $provider = new FlatFileProvider(__DIR__ . '/../../assets/data', ['abbr*']); $this->assertEquals(['Dr', 'Mr.', 'Mrs.', 'Ms.', 'Prof'], $provider->getValues()); - $provider = new FlatFileProvider(__DIR__.'/../../assets/data', ['*']); + $provider = new FlatFileProvider(__DIR__ . '/../../assets/data', ['*']); $this->assertEquals(['Dr', 'Mr.', 'Mrs.', 'Ms.', 'Prof'], $provider->getValues()); } } diff --git a/tests/src/GeneratorOffsetIteratorTest.php b/tests/src/GeneratorOffsetIteratorTest.php index ee8bd22..e49a373 100644 --- a/tests/src/GeneratorOffsetIteratorTest.php +++ b/tests/src/GeneratorOffsetIteratorTest.php @@ -13,7 +13,7 @@ public function testCanAccessPreviousAndNextValuesByOffset(): void { $generatorInvocationCount = 0; $yieldSequence = function () use (&$generatorInvocationCount) { - $generatorInvocationCount++; + ++$generatorInvocationCount; yield from range(100, 110); }; diff --git a/tests/src/ProbabilityCalculatorTest.php b/tests/src/ProbabilityCalculatorTest.php index 36f0852..7425951 100755 --- a/tests/src/ProbabilityCalculatorTest.php +++ b/tests/src/ProbabilityCalculatorTest.php @@ -5,13 +5,13 @@ namespace Bigwhoop\SentenceBreaker\Tests; use Bigwhoop\SentenceBreaker\Abbreviations\Abbreviations; +use Bigwhoop\SentenceBreaker\Lexing\Lexer; use Bigwhoop\SentenceBreaker\Lexing\Tokens\CapitalizedWordToken; use Bigwhoop\SentenceBreaker\Lexing\Tokens\EOFToken; -use Bigwhoop\SentenceBreaker\Lexing\Tokens\WordToken; use Bigwhoop\SentenceBreaker\Lexing\Tokens\WhitespaceToken; -use Bigwhoop\SentenceBreaker\Rules\IniConfiguration; +use Bigwhoop\SentenceBreaker\Lexing\Tokens\WordToken; use Bigwhoop\SentenceBreaker\ProbabilityCalculator; -use Bigwhoop\SentenceBreaker\Lexing\Lexer; +use Bigwhoop\SentenceBreaker\Rules\IniConfiguration; use PHPUnit\Framework\TestCase; class ProbabilityCalculatorTest extends TestCase @@ -88,8 +88,7 @@ public function dataAbbreviations(): array /** * @dataProvider dataSimpleSentences * - * @param string $input - * @param array $expectedResult + * @param array $expectedResult */ public function testSimpleSentences(string $input, array $expectedResult): void { @@ -99,8 +98,7 @@ public function testSimpleSentences(string $input, array $expectedResult): void /** * @dataProvider dataQuotes * - * @param string $input - * @param array $expectedResult + * @param array $expectedResult */ public function testQuotes(string $input, array $expectedResult): void { @@ -110,9 +108,8 @@ public function testQuotes(string $input, array $expectedResult): void /** * @dataProvider dataAbbreviations * - * @param string $input - * @param array $expectedResult - * @param array $abbreviations + * @param array $expectedResult + * @param array $abbreviations */ public function testAbbreviations(string $input, array $expectedResult, array $abbreviations): void { @@ -120,16 +117,15 @@ public function testAbbreviations(string $input, array $expectedResult, array $a } /** - * @param string $input - * @param array $expectedResult - * @param array $abbreviations + * @param array $expectedResult + * @param array $abbreviations */ private function runCalculateTest(string $input, array $expectedResult, array $abbreviations): void { $lexer = new Lexer(); $tokens = $lexer->run($input); - $rules = IniConfiguration::loadFile(__DIR__.'/../../rules/rules.ini')->getRules(); + $rules = IniConfiguration::loadFile(__DIR__ . '/../../rules/rules.ini')->getRules(); $calc = new ProbabilityCalculator($rules); $calc->setAbbreviations(new Abbreviations($abbreviations)); @@ -145,7 +141,7 @@ private function runCalculateTest(string $input, array $expectedResult, array $a continue; } - $actual[] = $token->getName().' '.$probability->getProbability(); + $actual[] = $token->getName() . ' ' . $probability->getProbability(); } $this->assertEquals($expectedResult, $actual); diff --git a/tests/src/Rules/IniConfigurationTest.php b/tests/src/Rules/IniConfigurationTest.php index ac9cb7a..800b139 100755 --- a/tests/src/Rules/IniConfigurationTest.php +++ b/tests/src/Rules/IniConfigurationTest.php @@ -5,18 +5,18 @@ namespace Bigwhoop\SentenceBreaker\Tests\Rules; use Bigwhoop\SentenceBreaker\Rules\ConfigurationException; +use Bigwhoop\SentenceBreaker\Rules\IniConfiguration; use Bigwhoop\SentenceBreaker\Rules\Rule; use Bigwhoop\SentenceBreaker\Rules\RulePattern; use Bigwhoop\SentenceBreaker\Rules\RulePatternToken; use Bigwhoop\SentenceBreaker\Rules\Rules; -use Bigwhoop\SentenceBreaker\Rules\IniConfiguration; use PHPUnit\Framework\TestCase; class IniConfigurationTest extends TestCase { public function testValidFile(): void { - $config = IniConfiguration::loadFile(__DIR__.'/../../assets/rules.ini'); + $config = IniConfiguration::loadFile(__DIR__ . '/../../assets/rules.ini'); $this->assertEquals(new Rules([ new Rule('T_EOF', [ diff --git a/tests/src/Rules/XMLConfigurationTest.php b/tests/src/Rules/XMLConfigurationTest.php index f519d4f..c7436a6 100755 --- a/tests/src/Rules/XMLConfigurationTest.php +++ b/tests/src/Rules/XMLConfigurationTest.php @@ -16,7 +16,7 @@ class XMLConfigurationTest extends TestCase { public function testValidFile(): void { - $config = XMLConfiguration::loadFile(__DIR__.'/../../assets/rules.xml'); + $config = XMLConfiguration::loadFile(__DIR__ . '/../../assets/rules.xml'); $this->assertEquals(new Rules([ new Rule('T_EOF', [ diff --git a/tests/src/SentenceBreakerPerformanceTest.php b/tests/src/SentenceBreakerPerformanceTest.php index d3a7e84..1b5a296 100755 --- a/tests/src/SentenceBreakerPerformanceTest.php +++ b/tests/src/SentenceBreakerPerformanceTest.php @@ -23,8 +23,8 @@ class SentenceBreakerPerformanceTest extends TestCase public function testCanAccessTheFirstSentenceOfLargeText(): void { $exampleSentences = $this->generateSentencesInRandomOrder(self::MAX_SENTENCES); - $firstSentence = $exampleSentences[0]; - $text = implode(' ', $exampleSentences); + $firstSentence = $exampleSentences[0]; + $text = implode(' ', $exampleSentences); unset($exampleSentences); $breaker = new SentenceBreaker(); diff --git a/tests/src/SentenceBreakerTest.php b/tests/src/SentenceBreakerTest.php index b6b1e9a..22f56d4 100755 --- a/tests/src/SentenceBreakerTest.php +++ b/tests/src/SentenceBreakerTest.php @@ -30,12 +30,13 @@ public function testPluralizedAbbreviation(): void /** * @dataProvider dataSentences + * * @param array $sentences */ public function testSplittingWithFlatFileProvider(string $text, array $sentences): void { $breaker = new SentenceBreaker(); - $breaker->addAbbreviations(new FlatFileProvider(__DIR__.'/../assets/data', ['*'])); + $breaker->addAbbreviations(new FlatFileProvider(__DIR__ . '/../assets/data', ['*'])); $this->assertSame($sentences, iterator_to_array($breaker->split($text))); } @@ -51,11 +52,11 @@ public function dataSentences(): array ['Hello Dr. Jones!', 'How are you?', "I'm fine, thanks!"], ], [ - 'Doctor, as a title, originates from the Latin word of the same spelling and meaning. The word is '. - "originally an agentive noun of the Latin verb docēre [dɔˈkeːrɛ] 'to teach'. It has been used as ". - 'an honored academic title for over a millennium in Europe, where it dates back to the rise of the '. - 'first universities. This use spread to the Americas, former European colonies, and is now prevalent '. - 'in most of the world. Contracted "Dr" or "Dr.", it is used as a designation for a person who has '. + 'Doctor, as a title, originates from the Latin word of the same spelling and meaning. The word is ' . + "originally an agentive noun of the Latin verb docēre [dɔˈkeːrɛ] 'to teach'. It has been used as " . + 'an honored academic title for over a millennium in Europe, where it dates back to the rise of the ' . + 'first universities. This use spread to the Americas, former European colonies, and is now prevalent ' . + 'in most of the world. Contracted "Dr" or "Dr.", it is used as a designation for a person who has ' . 'obtained a doctorate-level degree. Doctorates may be research doctorates or professional doctorates.', [ 'Doctor, as a title, originates from the Latin word of the same spelling and meaning.', @@ -70,14 +71,14 @@ public function dataSentences(): array 'He said: ‘Look at me, I am fancy.’ and the other replied “You really are!” True story ...', [ 'He said: ‘Look at me, I am fancy.’ and the other replied “You really are!”', - 'True story ...' + 'True story ...', ], ], [ 'Currently I am storing my bottles in the crates at a (about) 20 degree angle (bottles are upwards!).', [ 'Currently I am storing my bottles in the crates at a (about) 20 degree angle (bottles are upwards!).', - ] + ], ], [ "I'm a '90s kid. Yes, it's not 90's. I'm born in the '80s! The '80s, yes?", @@ -86,8 +87,8 @@ public function dataSentences(): array "Yes, it's not 90's.", "I'm born in the '80s!", "The '80s, yes?", - ] - ] + ], + ], ]; } } From 3e76243fe4907704e34452ce63b9009c6b96f273 Mon Sep 17 00:00:00 2001 From: Philippe Gerber Date: Fri, 21 Jul 2023 09:43:09 +0200 Subject: [PATCH 2/2] PHP 8.2 support, dropping 7.4 support --- .github/workflows/ci.yml | 2 +- composer.json | 6 +++--- phpstan.neon.dist | 1 - src/GeneratorOffsetIterator.php | 18 ++++++++++-------- src/Lexing/States/TextState.php | 3 --- src/Lexing/States/WhitespaceState.php | 3 --- src/Lexing/States/WordState.php | 3 --- src/Lexing/Tokens/PeriodToken.php | 3 --- src/ProbabilityCalculator.php | 5 ++--- src/Rules/IniConfiguration.php | 2 +- src/Rules/RulePattern.php | 1 - src/Rules/XMLConfiguration.php | 4 +--- src/SentenceBreaker.php | 7 +++---- src/SentenceBuilder.php | 6 ++---- 14 files changed, 23 insertions(+), 41 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8582770..1741264 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/composer.json b/composer.json index af66522..b1fa98e 100755 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 006892b..764b7d7 100755 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,4 @@ parameters: - phpVersion: 70400 # PHP 7.4 level: 9 paths: - src diff --git a/src/GeneratorOffsetIterator.php b/src/GeneratorOffsetIterator.php index 18d9ce1..857d322 100644 --- a/src/GeneratorOffsetIterator.php +++ b/src/GeneratorOffsetIterator.php @@ -4,19 +4,17 @@ namespace Bigwhoop\SentenceBreaker; -use Generator; use Iterator; /** * @template T + * * @implements Iterator */ -class GeneratorOffsetIterator implements Iterator +class GeneratorOffsetIterator implements \Iterator { - /** - * @var Generator - */ - private Generator $generator; + /** @var \Generator */ + private \Generator $generator; /** @var array */ private array $cache = []; @@ -24,14 +22,18 @@ class GeneratorOffsetIterator implements Iterator private int $currentIndex = 0; /** - * @param Generator $generator + * @param \Generator $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]; diff --git a/src/Lexing/States/TextState.php b/src/Lexing/States/TextState.php index f958272..a0d0e70 100755 --- a/src/Lexing/States/TextState.php +++ b/src/Lexing/States/TextState.php @@ -12,9 +12,6 @@ class TextState extends State { - /** - * {@inheritdoc} - */ protected function call(Lexer $lexer): ?State { while (true) { diff --git a/src/Lexing/States/WhitespaceState.php b/src/Lexing/States/WhitespaceState.php index 2d4dbe4..c12cdac 100755 --- a/src/Lexing/States/WhitespaceState.php +++ b/src/Lexing/States/WhitespaceState.php @@ -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)) { diff --git a/src/Lexing/States/WordState.php b/src/Lexing/States/WordState.php index ec87761..12c888a 100755 --- a/src/Lexing/States/WordState.php +++ b/src/Lexing/States/WordState.php @@ -18,9 +18,6 @@ private function getNonWordChars(): array return array_merge(['.', '?', '!', null], WhitespaceState::CHARS); } - /** - * {@inheritdoc} - */ protected function call(Lexer $lexer): ?State { $nonWordChars = $this->getNonWordChars(); diff --git a/src/Lexing/Tokens/PeriodToken.php b/src/Lexing/Tokens/PeriodToken.php index 5486f84..a8aaa53 100755 --- a/src/Lexing/Tokens/PeriodToken.php +++ b/src/Lexing/Tokens/PeriodToken.php @@ -11,9 +11,6 @@ public function getName(): string return 'T_PERIOD'; } - /** - * {@inheritdoc} - */ public function getPrintableValue(): string { return '.'; diff --git a/src/ProbabilityCalculator.php b/src/ProbabilityCalculator.php index 7fd3467..a73c564 100755 --- a/src/ProbabilityCalculator.php +++ b/src/ProbabilityCalculator.php @@ -10,7 +10,6 @@ use Bigwhoop\SentenceBreaker\Lexing\Tokens\Token; use Bigwhoop\SentenceBreaker\Rules\ConfigurationException; use Bigwhoop\SentenceBreaker\Rules\Rules; -use Generator; class ProbabilityCalculator { @@ -47,11 +46,11 @@ public function addRules(Rules $rules): void /** * @param iterable $tokens * - * @return Generator + * @return \Generator * * @throws ConfigurationException */ - public function calculate(iterable $tokens): Generator + public function calculate(iterable $tokens): \Generator { $tokenGenerator = function () use ($tokens) { foreach ($tokens as $token) { diff --git a/src/Rules/IniConfiguration.php b/src/Rules/IniConfiguration.php index d7567f8..052edbb 100755 --- a/src/Rules/IniConfiguration.php +++ b/src/Rules/IniConfiguration.php @@ -29,7 +29,7 @@ public static function loadFile(string $path): self } /** - @throws ConfigurationException + * @throws ConfigurationException */ public function __construct(string $data) { diff --git a/src/Rules/RulePattern.php b/src/Rules/RulePattern.php index 3449f4d..fe111fe 100755 --- a/src/Rules/RulePattern.php +++ b/src/Rules/RulePattern.php @@ -72,7 +72,6 @@ public function getTokensOffsetRelativeToStartToken(string $startTokenName): arr range(0, count($this->tokens) - 1) ); - /* @phpstan-ignore-next-line */ return array_combine($offsets, $this->tokens); } } diff --git a/src/Rules/XMLConfiguration.php b/src/Rules/XMLConfiguration.php index 98b3012..d14441e 100755 --- a/src/Rules/XMLConfiguration.php +++ b/src/Rules/XMLConfiguration.php @@ -4,11 +4,9 @@ namespace Bigwhoop\SentenceBreaker\Rules; -use SimpleXMLElement; - class XMLConfiguration implements Configuration { - private SimpleXMLElement $data; + private \SimpleXMLElement $data; /** * @throws ConfigurationException diff --git a/src/SentenceBreaker.php b/src/SentenceBreaker.php index 6aea620..2cd8ee3 100755 --- a/src/SentenceBreaker.php +++ b/src/SentenceBreaker.php @@ -13,7 +13,6 @@ use Bigwhoop\SentenceBreaker\Rules\ConfigurationException; use Bigwhoop\SentenceBreaker\Rules\IniConfiguration; use Bigwhoop\SentenceBreaker\Rules\Rules; -use Generator; class SentenceBreaker { @@ -29,7 +28,7 @@ class SentenceBreaker /** * @throws ConfigurationException */ - public function __construct(?Configuration $rulesConfig = null) + public function __construct(Configuration $rulesConfig = null) { $rules = $rulesConfig ? $rulesConfig->getRules() : $this->loadDefaultRules(); @@ -88,12 +87,12 @@ public function addAbbreviations($values): void } /** - * @return Generator + * @return \Generator * * @throws ConfigurationException * @throws Lexing\States\StateException */ - public function split(string $text): Generator + public function split(string $text): \Generator { $this->probabilityCalculator->setAbbreviations($this->getAbbreviations()); diff --git a/src/SentenceBuilder.php b/src/SentenceBuilder.php index 3a8e16f..b09bc43 100755 --- a/src/SentenceBuilder.php +++ b/src/SentenceBuilder.php @@ -4,16 +4,14 @@ namespace Bigwhoop\SentenceBreaker; -use Generator; - class SentenceBuilder { /** * @param iterable $tokenProbabilities * - * @return Generator + * @return \Generator */ - public function build(iterable $tokenProbabilities, int $threshold = 50): Generator + public function build(iterable $tokenProbabilities, int $threshold = 50): \Generator { $currentSentence = '';