<commit_before><?php
/*
 * PSX is an open source PHP framework to develop RESTful APIs.
 * For the current version and information visit <https://phpsx.org>
 *
 * Copyright 2010-2022 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace PSX\Framework\Api;

use PSX\Api\Listing\FilterInterface;
use PSX\Api\Listing\Route;
use PSX\Api\ListingInterface;
use PSX\Api\Parser\Attribute;
use PSX\Api\ResourceCollection;
use PSX\Api\Specification;
use PSX\Api\SpecificationInterface;
use PSX\Framework\Controller\ControllerAbstract;
use PSX\Framework\Loader\Context;
use PSX\Framework\Loader\PathMatcher;
use PSX\Framework\Loader\RoutingParserInterface;
use PSX\Schema\Definitions;
use PSX\Schema\SchemaManagerInterface;

/**
 * The documentation how a request and response looks is provided in the
 * controller
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @link    http://phpsx.org
 */
class ControllerDocumentation implements ListingInterface
{
    private RoutingParserInterface $routingParser;
    private Attribute $attributeParser;

    public function __construct(RoutingParserInterface $routingParser, SchemaManagerInterface $schemaManager)
    {
        $this->routingParser   = $routingParser;
        $this->attributeParser = new Attribute($schemaManager);
    }

    /**
     * @inheritdoc
     */
    public function getAvailableRoutes(FilterInterface $filter = null): iterable
    {
        $collections = $this->routingParser->getCollection($filter);
        $result      = array();

        foreach ($collections as $collection) {
            [$methods, $path, $source] = $collection;

            if ($filter !== null && !$filter->match($path)) {
                continue;
            }

            $parts = explode('::', $source, 2);
            $className = $parts[0] ?? null;

            if (is_a($className, ControllerAbstract::class, true)) {
                $result[] = new Route($path, $methods, '*');
            }
        }

        return $result;
    }

    /**
     * @inheritdoc
     */
    public function find(string $path, ?string $version = null): ?SpecificationInterface
    {
        $matcher    = new PathMatcher($path);
        $collection = $this->routingParser->getCollection();

        foreach ($collection as $route) {
            [$methods, $path, $source] = $route;

            $parts = explode('::', $source, 2);
            $className = $parts[0] ?? null;

            if (class_exists($className) && $matcher->match($path)) {
                return $this->attributeParser->parse($className, $path);
            }
        }

        return null;
    }

    /**
     * @inheritdoc
     */
    public function findAll(?string $version = null, FilterInterface $filter = null): SpecificationInterface
    {
        $spec  = new Specification(new ResourceCollection(), new Definitions());
        $index = $this->getAvailableRoutes($filter);

        foreach ($index as $resource) {
            $result = $this->find($resource->getPath(), $version);
            if ($result instanceof SpecificationInterface) {
                $spec->merge($result);
            }
        }

        return $spec;
    }

    /**
     * @param array $route
     * @return \PSX\Framework\Loader\Context
     */
    protected function newContext(array $route)
    {
        $context = new Context();
        $context->setPath($route[1]);

        return $context;
    }
}<commit_msg>update type hints<commit_after><?php
/*
 * PSX is an open source PHP framework to develop RESTful APIs.
 * For the current version and information visit <https://phpsx.org>
 *
 * Copyright 2010-2022 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace PSX\Framework\Api;

use PSX\Api\Listing\FilterInterface;
use PSX\Api\Listing\Route;
use PSX\Api\ListingInterface;
use PSX\Api\Parser\Attribute;
use PSX\Api\ResourceCollection;
use PSX\Api\Specification;
use PSX\Api\SpecificationInterface;
use PSX\Framework\Controller\ControllerAbstract;
use PSX\Framework\Loader\Context;
use PSX\Framework\Loader\PathMatcher;
use PSX\Framework\Loader\RoutingParserInterface;
use PSX\Schema\Definitions;
use PSX\Schema\SchemaManagerInterface;

/**
 * The documentation how a request and response looks is provided in the controller
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @link    http://phpsx.org
 */
class ControllerDocumentation implements ListingInterface
{
    private RoutingParserInterface $routingParser;
    private Attribute $attributeParser;

    public function __construct(RoutingParserInterface $routingParser, SchemaManagerInterface $schemaManager)
    {
        $this->routingParser   = $routingParser;
        $this->attributeParser = new Attribute($schemaManager);
    }

    public function getAvailableRoutes(?FilterInterface $filter = null): iterable
    {
        $collections = $this->routingParser->getCollection($filter);
        $result      = array();

        foreach ($collections as $collection) {
            [$methods, $path, $source] = $collection;

            if ($filter !== null && !$filter->match($path)) {
                continue;
            }

            $parts = explode('::', $source, 2);
            $className = $parts[0] ?? null;

            if (is_a($className, ControllerAbstract::class, true)) {
                $result[] = new Route($path, $methods, '*');
            }
        }

        return $result;
    }

    public function find(string $path, ?string $version = null): ?SpecificationInterface
    {
        $matcher    = new PathMatcher($path);
        $collection = $this->routingParser->getCollection();

        foreach ($collection as $route) {
            [$methods, $path, $source] = $route;

            $parts = explode('::', $source, 2);
            $className = $parts[0] ?? null;

            if (class_exists($className) && $matcher->match($path)) {
                return $this->attributeParser->parse($className, $path);
            }
        }

        return null;
    }

    public function findAll(?string $version = null, ?FilterInterface $filter = null): SpecificationInterface
    {
        $spec  = new Specification(new ResourceCollection(), new Definitions());
        $index = $this->getAvailableRoutes($filter);

        foreach ($index as $resource) {
            $result = $this->find($resource->getPath(), $version);
            if ($result instanceof SpecificationInterface) {
                $spec->merge($result);
            }
        }

        return $spec;
    }

    protected function newContext(array $route): Context
    {
        $context = new Context();
        $context->setPath($route[1]);

        return $context;
    }
}
{'fraction_non_alphanumeric': 0.12090453552614673, 'fraction_numerical': 0.004882436078632918, 'mean_word_length': 2.774975751697381, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '230427', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2071, 'n_words': 587}