Repository: symfony/symfony
File: src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php (PHP)

Code before:

    private function getGetter(object $subject): callable
    {
        $property = $this->property;
        $method = 'get'.ucfirst($property);

        return match (self::getType($subject, $property, $method)) {
            MarkingStoreMethod::METHOD => $subject->{$method}(...),
            MarkingStoreMethod::PROPERTY => static fn () => $subject->{$property},
        };
    }

    private function getSetter(object $subject): callable
    {
        $property = $this->property;
        $method = 'set'.ucfirst($property);

        return match (self::getType($subject, $property, $method, $type)) {
            MarkingStoreMethod::METHOD => $type ? static fn ($marking, $context) => $subject->{$method}($type::from($marking), $context) : $subject->{$method}(...),
            MarkingStoreMethod::PROPERTY => $type ? static fn ($marking) => $subject->{$property} = $type::from($marking) : static fn ($marking) => $subject->{$property} = $marking,
        };
    }

    private static function getSubjectIdentity(object $subject): string
    {
        return sprintf('%s_%s', $subject::class, spl_object_id($subject));
    }

    private static function getType(object $subject, string $property, string $method, ?string &$type = null): MarkingStoreMethod
    {
        try {
            if (method_exists($subject, $method) && ($r = new \ReflectionMethod($subject, $method))->isPublic()) {
                $type = 0 < $r->getNumberOfRequiredParameters() ? $r->getParameters()[0]->getType() : null;

                return MarkingStoreMethod::METHOD;
            }

            try {
                if (($r = new \ReflectionProperty($subject, $property))->isPublic()) {
                    $type = $r->getType();

                    return MarkingStoreMethod::PROPERTY;
                }
            } catch (\ReflectionException) {
            }

            throw new LogicException(\sprintf('Cannot store marking: class "%s" should have either a public method named "%s()" or a public property named "$%s"; none found.', get_debug_type($subject), $method, $property));
        } finally {
            $type = $type instanceof \ReflectionNamedType && is_subclass_of($type->getName(), \BackedEnum::class) ? $type->getName() : null;
        }
    }

Review comment (suggestion):
even this is not safe, as object ids can be reused after an object is destructed (which is likely to happen in the case of a messenger consumer, otherwise you would be leaking memory)

Code after:

    private function getGetter(object $subject): callable
    {
        $property = $this->property;
        $method = 'get'.ucfirst($property);

        return match (self::getType($subject, $property, $method)) {
            MarkingStoreMethod::METHOD => static fn ($subject) => $subject->{$method}(),
            MarkingStoreMethod::PROPERTY => static fn ($subject) => $subject->{$property},
        };
    }

    private function getSetter(object $subject): callable
    {
        $property = $this->property;
        $method = 'set'.ucfirst($property);

        return match (self::getType($subject, $property, $method, $type)) {
            MarkingStoreMethod::METHOD => $type ? static fn ($marking, $context) => $subject->{$method}($type::from($marking), $context) : $subject->{$method}(...),
            MarkingStoreMethod::PROPERTY => $type ? static fn ($marking) => $subject->{$property} = $type::from($marking) : static fn ($marking) => $subject->{$property} = $marking,
        };
    }

    private static function getType(object $subject, string $property, string $method, ?string &$type = null): MarkingStoreMethod
    {
        try {
            if (method_exists($subject, $method) && ($r = new \ReflectionMethod($subject, $method))->isPublic()) {
                $type = 0 < $r->getNumberOfRequiredParameters() ? $r->getParameters()[0]->getType() : null;

                return MarkingStoreMethod::METHOD;
            }

            try {
                if (($r = new \ReflectionProperty($subject, $property))->isPublic()) {
                    $type = $r->getType();

                    return MarkingStoreMethod::PROPERTY;
                }
            } catch (\ReflectionException) {
            }

            throw new LogicException(\sprintf('Cannot store marking: class "%s" should have either a public method named "%s()" or a public property named "$%s"; none found.', get_debug_type($subject), $method, $property));
        } finally {
            $type = $type instanceof \ReflectionNamedType && is_subclass_of($type->getName(), \BackedEnum::class) ? $type->getName() : null;
        }
    }
}

/**
 * @internal
 */