Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php (PHP)

Code before:
        $services = [];

        foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) {
            if (\in_array($serviceId, $exclude, true)) {
                continue;
            }

            $defaultPriority = null;
            $defaultIndex = null;
            $definition = $container->getDefinition($serviceId);
            $class = $definition->getClass();
            $class = $container->getParameterBag()->resolveValue($class) ?: null;
            $reflector = null !== $class ? $container->getReflectionClass($class) : null;
            $loadFromDefaultMethods = $reflector && null !== $defaultPriorityMethod;

            if ($reflector && $definition->isAutoconfigured() && !$definition->hasTag('container.ignore_attributes')) {
                $loadFromAttributes = $attributes !== $tagAttributes = array_filter($attributes);
                $attributes = [];
                foreach ($reflector->getAttributes(AsTaggedItem::class) as $attribute) {
                    $attribute = $attribute->newInstance();
                    $attributes[] = [
                        'priority' => $attribute->priority,
                        $indexAttribute ?? '' => $attribute->index,
                    ];
                    if (null === $defaultPriority) {
                        $defaultPriority = $attribute->priority ?? 0;
                        $defaultIndex = $attribute->index;
                    }
                }
                $attributes = $loadFromAttributes ? array_merge($attributes, $tagAttributes) : $tagAttributes;
            }

            foreach ($attributes as $attribute) {
                $index = $priority = null;

                if (isset($attribute['priority'])) {
                    $priority = $attribute['priority'];
                } elseif ($loadFromDefaultMethods) {
                    $defaultPriority = PriorityTaggedServiceUtil::getDefault($serviceId, $reflector, $defaultPriorityMethod, $tagName, 'priority') ?? $defaultPriority;
                    $defaultIndex = PriorityTaggedServiceUtil::getDefault($serviceId, $reflector, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute) ?? $defaultIndex;
                    $loadFromDefaultMethods = false;
                }
                $priority ??= $defaultPriority ??= 0;

                if (null === $indexAttribute && !$defaultIndexMethod && !$needsIndexes) {
                    $services[] = [$priority, ++$i, null, $serviceId, null];
                    continue 2;
                }

                if (null !== $indexAttribute && isset($attribute[$indexAttribute])) {
                    $index = $parameterBag->resolveValue($attribute[$indexAttribute]);

Review comment (suggestion):
only the first php-attribute can define a default priority and default index
and defaults defined by this first php-attribute have lower priority than defaults defined by static methods (getDefaultName et al.)

Note that we'd better deprecate static methods for defaults. php-attributes are cleaner IMHO. For 8.1 I suppose.

Code after:
        $services = [];

        foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) {
            if (\in_array($serviceId, $exclude, true)) {
                continue;
            }

            $defaultPriority = null;
            $defaultIndex = null;
            $definition = $container->getDefinition($serviceId);
            $class = $definition->getClass();
            $class = $container->getParameterBag()->resolveValue($class) ?: null;
            $reflector = null !== $class ? $container->getReflectionClass($class) : null;
            $loadFromDefaultMethods = $reflector && null !== $defaultPriorityMethod;
            $phpAttributes = $definition->isAutoconfigured() && !$definition->hasTag('container.ignore_attributes') ? $reflector?->getAttributes(AsTaggedItem::class) : [];

            foreach ($phpAttributes ??= [] as $i => $attribute) {
                $attribute = $attribute->newInstance();
                $phpAttributes[$i] = [
                    'priority' => $attribute->priority,
                    $indexAttribute ?? '' => $attribute->index,
                ];
                if (null === $defaultPriority) {
                    $defaultPriority = $attribute->priority ?? 0;
                    $defaultIndex = $attribute->index;
                }
            }
            if (1 >= \count($phpAttributes)) {
                $phpAttributes = [];
            }

            for ($i = 0; $i < \count($attributes); ++$i) {
                if (!($attribute = $attributes[$i]) && $phpAttributes) {
                    array_splice($attributes, $i--, 1, $phpAttributes);
                    continue;
                }

                $index = $priority = null;

                if (isset($attribute['priority'])) {
                    $priority = $attribute['priority'];
                } elseif ($loadFromDefaultMethods) {
                    $defaultPriority = PriorityTaggedServiceUtil::getDefault($serviceId, $reflector, $defaultPriorityMethod, $tagName, 'priority') ?? $defaultPriority;
                    $defaultIndex = PriorityTaggedServiceUtil::getDefault($serviceId, $reflector, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute) ?? $defaultIndex;
                    $loadFromDefaultMethods = false;
                }
                $priority ??= $defaultPriority ??= 0;

                if (null === $indexAttribute && !$defaultIndexMethod && !$needsIndexes) {
                    $services[] = [$priority, $i, null, $serviceId, null];
                    continue 2;