Skip to content

Fix SplObjectStorage deprecations on PHP 8.5#29

Merged
Metabor merged 1 commit into
masterfrom
fix/php85-splobjectstorage-deprecations
Jul 15, 2026
Merged

Fix SplObjectStorage deprecations on PHP 8.5#29
Metabor merged 1 commit into
masterfrom
fix/php85-splobjectstorage-deprecations

Conversation

@Metabor

@Metabor Metabor commented Jul 15, 2026

Copy link
Copy Markdown
Owner

PHP 8.5 deprecates SplObjectStorage::attach() and ::detach(). Fifteen call sites in src/ trigger them, so every consumer running on PHP 8.5 gets deprecation notices coming out of this library.

What changes

attach($o)offsetSet($o, null) and detach($o)offsetUnset($o) — the replacements named in the deprecation message.

File Lines
Metabor/Callback/Composite.php 30, 35
Metabor/Event/Event.php 51, 56
Metabor/KeyValue/Composite.php 19, 24
Metabor/Observer/Subject.php 20, 25
Metabor/Statemachine/State.php 39
Metabor/Statemachine/Factory/Factory.php 44, 49
Metabor/Statemachine/Graph/GraphBuilder.php 91, 99
Metabor/Statemachine/Observer/SubjectHasUpdated.php 15, 17

Plus the four occurrences in tests/.

Three attach() calls in src/ are deliberately left alone. They target Event and Statemachine, which define their own attach() — not SplObjectStorage: SetupHelper.php:111, StateCollectionMerger.php:149, Factory.php:66. A blind search-and-replace would have broken the public API here.

Equivalence

attach($o) stores null as its $info by default, and offsetSet($o, null) does the same — identical in behaviour, including return value, repeated insertion, count(), contains() and iteration. $info is never read anywhere in this repository.

Verification

The test suite is not sufficient evidence here, and that is worth spelling out: it only reports deprecations on the code paths it executes. Factory::attachStatemachineObserver, Factory::detachStatemachineObserver, Event::detach, KeyValue\Composite::detach, GraphBuilder and one branch of SubjectHasUpdated are reached by none of the 88 tests. A fix driven by the suite output stops at eight of the fifteen — and the suite then reports success.

Three independent checks on PHP 8.5.4:

1. Static — independent of any execution

$ grep -rn -- "->attach(\|->detach(" src/ | grep -v "public function"
3 hits, all three the intentional Event/Statemachine calls listed above

2. The uncovered paths, driven by hand

A script calling Factory::attachStatemachineObserver, Factory::detachStatemachineObserver and Event::detach — none of which any test reaches:

master:      4 deprecations  (Factory.php:44, Factory.php:49, Event.php:51, Event.php:56)
this branch: 0

3. The suite, for what it is worth

before:  Tests: 88, Assertions: 189, Deprecations: 12
after:   OK (88 tests, 189 assertions)

Behaviour

Running Statemachine-Example against the patched library produces byte-identical output (76 lines, 3630 bytes), and drops it from 21 deprecations per run to 0. Those 21 only surface under error_reporting=E_ALL; the stock php.ini hides them, which is part of why they went unnoticed.

Not addressed here

SubjectHasUpdated extends \SplObjectStorage, so the class inherits the deprecated attach()/detach() into its own public API. Both internal calls are fixed, but the inheritance is a design question, not a deprecation fix.

SplObjectStorage::attach() and ::detach() are deprecated since PHP 8.5.
All 15 call sites in src/ that target an SplObjectStorage now use the
documented replacements, offsetSet($object, null) and offsetUnset($object).

  Callback/Composite.php                     30, 35
  Event/Event.php                            51, 56
  KeyValue/Composite.php                     19, 24
  Observer/Subject.php                       20, 25
  Statemachine/State.php                     39
  Statemachine/Factory/Factory.php           44, 49
  Statemachine/Graph/GraphBuilder.php        91, 99
  Statemachine/Observer/SubjectHasUpdated.php 15, 17

Three attach() calls in src/ are left untouched on purpose — they target
Event and Statemachine, which have their own attach(), not SplObjectStorage:
SetupHelper.php:111, StateCollectionMerger.php:149, Factory.php:66.

Equivalence: attach($o) with its default $info also stores null, so
offsetSet($o, null) is identical in behaviour. $info is never used
anywhere in this repository.

Also fixes the four occurrences in tests/.

Verified on PHP 8.5.4:
  static:   no SplObjectStorage attach/detach left in src/
  suite:    12 deprecations -> OK (88 tests, 189 assertions)
  uncovered paths (Factory, Event::detach — no test reaches them):
            4 deprecations -> 0
  example:  byte-identical output, 76 lines / 3630 bytes
@Metabor
Metabor force-pushed the fix/php85-splobjectstorage-deprecations branch from 02eda2b to 0a74881 Compare July 15, 2026 13:09
@Metabor
Metabor merged commit 0bf2258 into master Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant