Skip to content

PHP 8.5 Deprecation: SplObjectStorage::contains() deprecated in RemoveSpansWithoutAttributes.php #476

@javiermarinros

Description

@javiermarinros

Summary
When running the application on PHP 8.5, a notice is triggered regarding the deprecation of SplObjectStorage::contains().

Error Message

Method SplObjectStorage::contains() is deprecated since 8.5, use method SplObjectStorage::offsetExists() instead

Affected File
library/HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php at line 88 (inside handleEnd).

Analysis
The SplObjectStorage class usage has changed in newer PHP versions. The contains method is being deprecated in favor of offsetExists.

Proposed Fix

Update the handleEnd method to use offsetExists.

Current Code (implied by error):

public function handleEnd(&$token)
{
    if ($this->markForDeletion->contains($token)) {
        $this->markForDeletion->detach($token);
        $token = false;
    }
}

Suggested Change:

public function handleEnd(&$token)
{
    if ($this->markForDeletion->offsetExists($token)) {
        $this->markForDeletion->offsetUnset($token);
        $token = false;
    }
}

Note: offsetUnset should also be used as detach is similarly affected, or for consistency with offsetExists.
And the same for line 77, markForDeletion->attach should be markForDeletion->offsetSet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions