<commit_before><?php

namespace CirclicalAutoWire\Annotations;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;

/**
 * @Annotation
 * @Target("METHOD")
 */
class Route
{
    /**
     * @Required
     * @var string
     */
    public $value;

    /**
     * @var string
     */
    public $type;


    /**
     * @var array
     */
    public $constraints;


    /**
     * @var array
     */
    public $defaults;


    public function transform(string $controllerClass, string $methodName): array
    {
        $route = [
            'type' => $this->type ?? $this->identifyRoute($this->value),
            'options' => [
                'route' => $this->value,
                'defaults' => [
                    'controller' => $controllerClass,
                    'action' => preg_replace('/Action$/', '', $methodName),
                ],
            ],
        ];

        if( $this->constraints ){
            $route['options']['constraints'] = $this->constraints;
        }

        if( $this->defaults ){
            $route['options']['defaults'] = $this->defaults;
        }

        return $route;
    }

    private function identifyRoute(string $route): string
    {
        if (strpos($route, ':') !== false) {
            return Segment::class;
        }

        return Literal::class;
    }

}<commit_msg>Segment matching on detection of [ as well for optional parameters.<commit_after><?php

namespace CirclicalAutoWire\Annotations;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;

/**
 * @Annotation
 * @Target("METHOD")
 */
class Route
{
    /**
     * @Required
     * @var string
     */
    public $value;

    /**
     * @var string
     */
    public $type;


    /**
     * @var array
     */
    public $constraints;


    /**
     * @var array
     */
    public $defaults;


    public function transform(string $controllerClass, string $methodName): array
    {
        $route = [
            'type' => $this->type ?? $this->identifyRoute($this->value),
            'options' => [
                'route' => $this->value,
                'defaults' => [
                    'controller' => $controllerClass,
                    'action' => preg_replace('/Action$/', '', $methodName),
                ],
            ],
        ];

        if( $this->constraints ){
            $route['options']['constraints'] = $this->constraints;
        }

        if( $this->defaults ){
            $route['options']['defaults'] = $this->defaults;
        }

        return $route;
    }

    private function identifyRoute(string $route): string
    {
        if (strpos($route, ':') !== false || strpos($route, '[') !== false) {
            return Segment::class;
        }

        return Literal::class;
    }

}
{'fraction_non_alphanumeric': 0.16020297209133744, 'fraction_numerical': 0.0, 'mean_word_length': 1.6926829268292682, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3943746', 'n_tokens_mistral': 810, 'n_tokens_neox': 769, 'n_words': 151}