Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ updates:
interval: daily
open-pull-requests-limit: 10
versioning-strategy: widen
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
8 changes: 4 additions & 4 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ jobs:
- 8.5

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v7

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Validate composer.json and composer.lock
run: composer validate
- name: Validate composer.json
run: composer validate --strict

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
uses: actions/cache@v6
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
/coverage.xml
/bin
/coverage/
/.php-cs-fixer.cache
29 changes: 29 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

$finder = (new PhpCsFixer\Finder())
->in(__DIR__ . '/src')
->in(__DIR__ . '/spec')
->in(__DIR__ . '/tests')
->append([__FILE__]);

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PER-CS' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP82Migration' => true,
'@PHP82Migration:risky' => true,
'binary_operator_spaces' => ['operators' => ['=>' => 'align_single_space_minimal', '=' => 'align_single_space_minimal']],
'concat_space' => ['spacing' => 'one'],
'global_namespace_import' => ['import_classes' => true, 'import_constants' => false, 'import_functions' => false],
'native_constant_invocation' => false,
'native_function_invocation' => false,
'ordered_class_elements' => true,
'phpdoc_var_without_name' => false,
'self_accessor' => false,
'single_line_throw' => false,
])
->setFinder($finder);
29 changes: 0 additions & 29 deletions .php_cs.dist

This file was deleted.

27 changes: 0 additions & 27 deletions Dockerfile

This file was deleted.

13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"type": "library",
"require": {
"php": "8.2.* || 8.3.* || 8.4.* || 8.5.*",
"ext-libxml": "*"
"ext-mbstring": "*"
},
"require-dev": {
"ext-mbstring": "*",
"phpspec/phpspec": "8.3.1",
"phpunit/phpunit": "9.6.* || 10.5.*",
"friends-of-phpspec/phpspec-code-coverage": "7.0.0",
"phpstan/phpstan": "1.10.* || 1.11.* || 1.12.*",
"phpstan/extension-installer": "1.3.* || 1.4.*",
"jangregor/phpstan-prophecy": "1.0.*"
"jangregor/phpstan-prophecy": "1.0.*",
"friendsofphp/php-cs-fixer": "3.95.*"
},
"license": "MIT",
"authors": [
Expand All @@ -32,8 +32,11 @@
"test": [
"vendor/bin/phpspec run",
"vendor/bin/phpunit tests/",
"vendor/bin/phpstan analyse"
]
"vendor/bin/phpstan analyse",
"@cs"
],
"cs": "vendor/bin/php-cs-fixer fix --dry-run --diff",
"cs-fix": "vendor/bin/php-cs-fixer fix"
},
"config": {
"allow-plugins": {
Expand Down
11 changes: 0 additions & 11 deletions docker/xdebug.ini

This file was deleted.

12 changes: 6 additions & 6 deletions spec/Lexer/LexerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function it_gives_the_same_tokens_when_tokenising_the_same_text_twice():
$origState = new InitialState();
$newState = new InitialState();

$origState->registerMatcher(new LexerSpecRegexMatcher('Letter', '/(?<all>a)/A'), function (Lexer $lexer) use ($newState): void {
$origState->registerMatcher(new LexerSpecRegexMatcher('Letter', '/(?<all>a)/A'), static function (Lexer $lexer) use ($newState): void {
$lexer->pushState($newState);
});
$newState->registerMatcher(new LexerSpecRegexMatcher('Other', '/(?<all>a)/A'));
Expand All @@ -138,7 +138,7 @@ public function it_restores_the_initial_state_after_tokenising(): void
$origState = new InitialState();
$newState = new InitialState();

$origState->registerMatcher(new LexerSpecRegexMatcher('Letter', '/(?<all>a)/A'), function (Lexer $lexer) use ($newState): void {
$origState->registerMatcher(new LexerSpecRegexMatcher('Letter', '/(?<all>a)/A'), static function (Lexer $lexer) use ($newState): void {
$lexer->pushState($newState);
});

Expand Down Expand Up @@ -169,7 +169,7 @@ public function it_switches_state_on_a_zero_length_lookahead_match(): void
$newState = new InitialState();

$origState->registerMatcher(new LexerSpecRegexMatcher('Letter', '/(?<all>a)/A'));
$origState->registerMatcher(new LexerSpecRegexMatcher('Lookahead', '/(?<all>)(?=b)/A'), function (Lexer $lexer) use ($newState): void {
$origState->registerMatcher(new LexerSpecRegexMatcher('Lookahead', '/(?<all>)(?=b)/A'), static function (Lexer $lexer) use ($newState): void {
$lexer->pushState($newState);
});
$newState->registerMatcher(new LexerSpecRegexMatcher('Rest', '/(?<all>b+)/A'));
Expand All @@ -194,10 +194,10 @@ public function it_throws_if_zero_length_matches_return_to_a_state_without_advan
$firstState = new InitialState();
$secondState = new InitialState();

$firstState->registerMatcher(new LexerSpecRegexMatcher('ToSecond', '/(?<all>)/A'), function (Lexer $lexer) use ($secondState): void {
$firstState->registerMatcher(new LexerSpecRegexMatcher('ToSecond', '/(?<all>)/A'), static function (Lexer $lexer) use ($secondState): void {
$lexer->pushState($secondState);
});
$secondState->registerMatcher(new LexerSpecRegexMatcher('ToFirst', '/(?<all>)/A'), function (Lexer $lexer) use ($firstState): void {
$secondState->registerMatcher(new LexerSpecRegexMatcher('ToFirst', '/(?<all>)/A'), static function (Lexer $lexer) use ($firstState): void {
$lexer->pushState($firstState);
});

Expand Down Expand Up @@ -227,6 +227,6 @@ public function match(string $text): ?MatchedText

public function getName(): string
{
return $this->type.'Matcher';
return $this->type . 'Matcher';
}
}
1 change: 0 additions & 1 deletion spec/NodeTraverser/NodeTraverserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace spec\chrisjenkinson\StructuredDocumentParser\NodeTraverser;

use chrisjenkinson\StructuredDocumentParser\Node\NodeInterface;
use chrisjenkinson\StructuredDocumentParser\NodeTraverser\NodeTraverser;
use chrisjenkinson\StructuredDocumentParser\NodeVisitor\NodeVisitorAction;
use chrisjenkinson\StructuredDocumentParser\NodeVisitor\NodeVisitorInterface;
use PhpSpec\ObjectBehavior;
Expand Down
2 changes: 1 addition & 1 deletion spec/State/InitialStateSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function it_calls_a_callback(Lexer $lexer, Cursor $cursor, MatcherInterfa

$lexer->getState()->shouldBeCalled();

$this->registerMatcher($matcher, function (Lexer $lexer): void {
$this->registerMatcher($matcher, static function (Lexer $lexer): void {
$lexer->getState();
});

Expand Down
2 changes: 1 addition & 1 deletion spec/State/NoTokenFoundExceptionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function it_caps_the_excerpt_at_100_characters(): void
{
$this->beConstructedWith('stateName', 50, str_repeat('é', 150), new TokenPosition(12, 5));

$this->getMessage()->shouldReturn('No token found with state stateName at line 12, column 5: "'.str_repeat('é', 100).'…"');
$this->getMessage()->shouldReturn('No token found with state stateName at line 12, column 5: "' . str_repeat('é', 100) . '…"');
}

public function it_shows_a_newline_when_the_text_starts_with_one(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Lexer/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function advance(int $length): void
$newlines = mb_substr_count($consumed, "\n");

if (0 < $newlines) {
$this->line += $newlines;
$this->line += $newlines;
$this->column = mb_strlen($consumed) - mb_strrpos($consumed, "\n");
} else {
$this->column += mb_strlen($consumed);
Expand Down
2 changes: 0 additions & 2 deletions src/Matcher/MatchedText.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public function __construct(private readonly array $matches)
}

/**
* @param string $key
*
* @return mixed|null
*/
public function get(string $key)
Expand Down
3 changes: 0 additions & 3 deletions src/Matcher/MatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ interface MatcherInterface
*/
public function match(string $text): ?MatchedText;

/**
* @return string
*/
public function getName(): string;
}
9 changes: 4 additions & 5 deletions src/NodeTraverser/NodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function addVisitor(NodeVisitorInterface $visitor): void
*/
public function traverse(NodeInterface $node): ?NodeInterface
{
array_map(function (NodeVisitorInterface $nodeVisitor) use (&$node): void {
array_map(static function (NodeVisitorInterface $nodeVisitor) use (&$node): void {
if (null === $before = $nodeVisitor->beforeTraverse($node)) {
return;
}
Expand All @@ -40,7 +40,7 @@ public function traverse(NodeInterface $node): ?NodeInterface
return null;
}

array_map(function (NodeVisitorInterface $nodeVisitor) use (&$node): void {
array_map(static function (NodeVisitorInterface $nodeVisitor) use (&$node): void {
if (null === $after = $nodeVisitor->afterTraverse($node)) {
return;
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public function traverseChildren(array $children): array

private function runEnterNodeVisitors(NodeInterface $node): NodeInterface
{
array_map(function (NodeVisitorInterface $nodeVisitor) use (&$node): void {
array_map(static function (NodeVisitorInterface $nodeVisitor) use (&$node): void {
if (null === $enter = $nodeVisitor->enterNode($node)) {
return;
}
Expand All @@ -107,7 +107,6 @@ private function runEnterNodeVisitors(NodeInterface $node): NodeInterface
}

/**
* @param NodeInterface $node
* @param NodeInterface[] $children
*/
private function runTraverseNodeOnSubNodes(NodeInterface $node, array $children): void
Expand Down Expand Up @@ -151,7 +150,7 @@ private function runTraverseChildrenOnAttributes(NodeInterface $node, array $att

private function runLeaveNodeVisitors(NodeInterface $node): NodeInterface|NodeVisitorAction
{
array_map(function (NodeVisitorInterface $nodeVisitor) use (&$node): void {
array_map(static function (NodeVisitorInterface $nodeVisitor) use (&$node): void {
if (NodeVisitorAction::RemoveNode === $node) {
return;
}
Expand Down
12 changes: 0 additions & 12 deletions src/NodeVisitor/AbstractNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,21 @@

abstract class AbstractNodeVisitor implements NodeVisitorInterface
{
/**
* {@inheritdoc}
*/
public function beforeTraverse(NodeInterface $node): ?NodeInterface
{
return null;
}

/**
* {@inheritdoc}
*/
public function afterTraverse(NodeInterface $node): ?NodeInterface
{
return null;
}

/**
* {@inheritdoc}
*/
public function enterNode(NodeInterface $node): ?NodeInterface
{
return null;
}

/**
* {@inheritdoc}
*/
public function leaveNode(NodeInterface $node): NodeInterface|NodeVisitorAction|null
{
return null;
Expand Down
14 changes: 7 additions & 7 deletions src/State/AbstractState.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function findMatchingToken(Lexer $lexer, Cursor $cursor): TokenInterface
);
}

public function getName(): string
{
return (new ReflectionClass($this))->getShortName();
}

private function getTokenType(string $matcherName): string
{
if (str_ends_with($matcherName, 'Matcher')) {
Expand Down Expand Up @@ -79,8 +84,8 @@ private function findSingleMatch(array $matches, string $remainingText, int $cur
throw new AmbiguousTokenFoundException(
$this->getName(),
$remainingText,
array_map(fn (MatcherMatch $match): string => $match->matcherName, $matches),
array_map(fn (MatcherMatch $match): MatchedText => $match->matchedText, $matches),
array_map(static fn (MatcherMatch $match): string => $match->matcherName, $matches),
array_map(static fn (MatcherMatch $match): MatchedText => $match->matchedText, $matches),
$position
);
}
Expand Down Expand Up @@ -117,9 +122,4 @@ private function runMatchers(string $text): array

return $matches;
}

public function getName(): string
{
return (new ReflectionClass($this))->getShortName();
}
}
Loading
Loading