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
35 changes: 1 addition & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
php: ["8.2", "8.3", "8.4", "8.5"]

steps:
- name: Checkout
Expand Down Expand Up @@ -111,36 +111,3 @@ jobs:

- name: PHP-CS-Fixer (dry-run + diff)
run: vendor/bin/php-cs-fixer fix --dry-run --diff

lowest-php-syntax:
name: Syntax · PHP ${{ matrix.php }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ["5.6", "7.0", "7.1", "7.2"]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
# No Composer install on these PHP versions: PHPUnit 9.6
# requires PHP >= 7.3, but composer.json keeps a "php: >=5.6"
# contract for library consumers. We only verify that the
# source files themselves parse cleanly on these interpreters.

- name: Lint src/
run: find src -type f -name '*.php' -print0 | xargs -0 -n1 php -l

- name: Autoload smoke test
# Standalone script (not `php -r`) — on PHP 5.6 / 7.0 / 7.1 / 7.2
# `__DIR__` is not defined inside `php -r '...'`, which made the
# autoloader resolve `/src/...` and load nothing. As a file
# `__DIR__` reliably points at tests/compat/, so the relative
# path to src/ works on every PHP version we support.
run: php tests/compat/autoload-smoke.php
120 changes: 32 additions & 88 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,101 +1,45 @@
<?php

/**
* PHP-CS-Fixer configuration for initphp/events.
*
* Baseline: PSR-12. The handful of additions below codify the style
* the existing source already uses (short array syntax, ordered
* imports, single quotes, trailing commas in multi-line arrays).
declare(strict_types=1);

/*
* Shared php-cs-fixer configuration for every InitPHP package (ADR-005).
*
* Important: rules that would inject modern type-system syntax
* (declare_strict_types, return type declarations, void return,
* nullable type declarations) are intentionally *off* because the
* runtime contract in composer.json is `php: >= 5.6` and those
* constructs would silently break that promise.
* This file is identical in all repositories as `.php-cs-fixer.dist.php`.
* The only package-specific part is the two lines below; scripts/check-cs-config.sh
* ignores exactly those two lines when it compares a package with this template.
* Keep each of them on a single line.
*/
$excludeDirs = [];
$excludePaths = [];

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
->in([__DIR__ . '/src', __DIR__ . '/tests'])
->name('*.php');

foreach ($excludeDirs as $excludeDir) {
$finder->exclude($excludeDir);
}

foreach ($excludePaths as $excludePath) {
$finder->notPath($excludePath);
}

return (new PhpCsFixer\Config())
->setRiskyAllowed(false)
->setUsingCache(true)
->setCacheFile(__DIR__ . '/.php-cs-fixer.cache')
->setFinder($finder)
->setRules([
'@PSR12' => true,

// PSR-12 makes constant visibility (`public const`) mandatory,
// but that syntax requires PHP 7.1+. We support PHP 5.6, so
// we restrict the modifier-keywords fixer (the new name for
// the deprecated `visibility_required`) to method/property
// only and leave bare `const` declarations alone.
'modifier_keywords' => ['elements' => ['method', 'property']],

// Keep empty closure bodies (`function () {}`) on one line —
// they read better than the 2-line equivalent. Non-empty
// single-line bodies still get expanded by PSR-12's
// statement_indentation rule, which is fine.
'single_line_empty_body' => true,
'braces_position' => [
'anonymous_functions_opening_brace' => 'same_line',
],

// PHPDoc separation produces noisy blank lines around
// @return / @throws / @param. The existing house style packs
// them together; keep that.
'phpdoc_separation' => false,

// Array & syntax preferences (match the existing source).
'array_syntax' => ['syntax' => 'short'],
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
'no_whitespace_before_comma_in_array' => true,
'whitespace_after_comma_in_array' => true,

// Imports.
'no_unused_imports' => true,
'ordered_imports' => [
'@PER-CS' => true,
'@PHP8x2Migration' => true,
'array_syntax' => ['syntax' => 'short'],
'no_unused_imports' => true,
'ordered_imports' => [
'imports_order' => ['class', 'function', 'const'],
'sort_algorithm' => 'alpha',
'imports_order' => ['class', 'function', 'const'],
],

// Strings.
'single_quote' => true,

// Whitespace.
'blank_line_after_opening_tag' => true,
'no_extra_blank_lines' => [
'tokens' => [
'extra',
'throw',
'use',
'curly_brace_block',
'parenthesis_brace_block',
'square_brace_block',
],
'single_quote' => true,
'trailing_comma_in_multiline' => [
'elements' => ['arguments', 'array_destructuring', 'arrays', 'match', 'parameters'],
],
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'single_blank_line_at_eof' => true,

// Operators.
'binary_operator_spaces' => ['default' => 'single_space'],
'concat_space' => ['spacing' => 'one'],
'not_operator_with_successor_space' => false,
'unary_operator_spaces' => true,

// Phpdoc.
'phpdoc_align' => ['align' => 'left'],
'phpdoc_indent' => true,
'phpdoc_no_useless_inheritdoc' => false, // @inheritDoc is meaningful for IDE/PHPStan in our interface impls.
'phpdoc_scalar' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
]);
])
->setFinder($finder)
->setCacheFile(__DIR__ . '/build/php-cs-fixer.cache');
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Breaking changes

- **Requires PHP `^8.2`** (was `>=5.6`); PHP 5.6 through 8.1 are no longer
supported. CI now covers PHP 8.2, 8.3, 8.4 and 8.5, and the PHP 5.6 – 7.2
syntax-lint job has been removed.
- Code style moved to PER-CS (php-cs-fixer, shared `.php-cs-fixer.dist.php`,
replacing the previous PSR-12 configuration); the codebase has been
reformatted accordingly.

## [2.1.0] - 2026-05-25

The entries below cover the whole 2.x line (2.0.0 shipped without a changelog).

### Breaking changes

- **`EventEmitter` now honours priority.** Listeners are dispatched in
ascending numeric priority order regardless of the order in which
they were registered. In 1.x they ran in registration order; the
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ zero runtime dependencies.

| Requirement | Version |
| --- | --- |
| PHP | `>= 5.6` (runtime); PHP `>= 7.3` to run the test suite (PHPUnit 9.6) |
| PHP | `>= 8.2` |
| Extensions | none |
| Composer dependencies | none |

Expand Down Expand Up @@ -324,9 +324,7 @@ composer install
composer test # PHPUnit
```

CI runs the test suite across PHP 7.3 – 8.4 and also lints the source
on PHP 5.6 / 7.0 / 7.1 / 7.2 so the `php >= 5.6` library contract stays
honest.
CI runs the test suite across PHP 8.2 – 8.5.

## Contributing & Security

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"issues": "https://github.com/InitPHP/Events/issues"
},
"require": {
"php": ">=5.6"
"php": "^8.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"friendsofphp/php-cs-fixer": "^3.88",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^9.6"
},
Expand Down
6 changes: 3 additions & 3 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
*/
final class Event
{
const PRIORITY_LOW = 200;
const PRIORITY_NORMAL = 100;
const PRIORITY_HIGH = 10;
public const PRIORITY_LOW = 200;
public const PRIORITY_NORMAL = 100;
public const PRIORITY_HIGH = 10;

/** @var EventEmitter */
protected $emitter;
Expand Down
6 changes: 3 additions & 3 deletions src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
*/
class Events
{
const PRIORITY_LOW = Event::PRIORITY_LOW;
const PRIORITY_NORMAL = Event::PRIORITY_NORMAL;
const PRIORITY_HIGH = Event::PRIORITY_HIGH;
public const PRIORITY_LOW = Event::PRIORITY_LOW;
public const PRIORITY_NORMAL = Event::PRIORITY_NORMAL;
public const PRIORITY_HIGH = Event::PRIORITY_HIGH;

/** @var Event|null */
protected static $Instance;
Expand Down
4 changes: 2 additions & 2 deletions src/aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
if (!class_exists('InitPHP\\EventEmitter\\EventEmitter', false)) {
class_alias(
\InitPHP\Events\EventEmitter::class,
'InitPHP\\EventEmitter\\EventEmitter'
'InitPHP\\EventEmitter\\EventEmitter',
);
}

if (!interface_exists('InitPHP\\EventEmitter\\EventEmitterInterface', false)) {
class_alias(
\InitPHP\Events\EventEmitterInterface::class,
'InitPHP\\EventEmitter\\EventEmitterInterface'
'InitPHP\\EventEmitter\\EventEmitterInterface',
);
}
4 changes: 2 additions & 2 deletions tests/BackwardsCompatibilityAliasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public function test_legacy_event_emitter_class_alias_is_registered(): void
{
$this->assertTrue(
class_exists('InitPHP\\EventEmitter\\EventEmitter'),
'The legacy class \\InitPHP\\EventEmitter\\EventEmitter must remain available via the BC alias.'
'The legacy class \\InitPHP\\EventEmitter\\EventEmitter must remain available via the BC alias.',
);
}

public function test_legacy_event_emitter_interface_alias_is_registered(): void
{
$this->assertTrue(
interface_exists('InitPHP\\EventEmitter\\EventEmitterInterface'),
'The legacy interface \\InitPHP\\EventEmitter\\EventEmitterInterface must remain available via the BC alias.'
'The legacy interface \\InitPHP\\EventEmitter\\EventEmitterInterface must remain available via the BC alias.',
);
}

Expand Down
34 changes: 25 additions & 9 deletions tests/EventEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,23 @@ public function test_listeners_run_in_ascending_priority_order_regardless_of_reg
$this->assertSame(
['priority-10', 'priority-99', 'priority-100'],
$order,
'Listeners must execute in ascending priority order regardless of the order in which they were registered.'
'Listeners must execute in ascending priority order regardless of the order in which they were registered.',
);
}

public function test_listeners_at_the_same_priority_run_in_registration_order_fifo(): void
{
$order = [];

$this->emitter->on('e', function () use (&$order): void { $order[] = 'first'; }, 50);
$this->emitter->on('e', function () use (&$order): void { $order[] = 'second'; }, 50);
$this->emitter->on('e', function () use (&$order): void { $order[] = 'third'; }, 50);
$this->emitter->on('e', function () use (&$order): void {
$order[] = 'first';
}, 50);
$this->emitter->on('e', function () use (&$order): void {
$order[] = 'second';
}, 50);
$this->emitter->on('e', function () use (&$order): void {
$order[] = 'third';
}, 50);

$this->emitter->emit('e');

Expand Down Expand Up @@ -276,8 +282,12 @@ public function test_clear_once_listeners_for_a_specific_event_drops_only_that_e
$aFires = 0;
$bFires = 0;

$this->emitter->once('a', function () use (&$aFires): void { $aFires++; });
$this->emitter->once('b', function () use (&$bFires): void { $bFires++; });
$this->emitter->once('a', function () use (&$aFires): void {
$aFires++;
});
$this->emitter->once('b', function () use (&$bFires): void {
$bFires++;
});

$this->emitter->clearOnceListeners('a');

Expand All @@ -292,10 +302,16 @@ public function test_clear_once_listeners_with_no_argument_drops_every_one_shot_
{
$fired = 0;

$this->emitter->once('a', function () use (&$fired): void { $fired++; });
$this->emitter->once('b', function () use (&$fired): void { $fired++; });
$this->emitter->once('a', function () use (&$fired): void {
$fired++;
});
$this->emitter->once('b', function () use (&$fired): void {
$fired++;
});
// Regular listener must NOT be touched.
$this->emitter->on('c', function () use (&$fired): void { $fired++; });
$this->emitter->on('c', function () use (&$fired): void {
$fired++;
});

$this->emitter->clearOnceListeners();

Expand Down
Loading
Loading