<commit_before><?php
/**
 * Bright Nucleus View Component.
 *
 * @package   BrightNucleus\View
 * @author    <NAME> <<EMAIL>>
 * @license   MIT
 * @link      http://www.brightnucleus.com/
 * @copyright 2016 <NAME>, Bright Nucleus
 */

namespace BrightNucleus\View\Location;

use Exception;

/**
 * Class FilesystemLocation.
 *
 * @since   0.1.0
 *
 * @package BrightNucleus\View\Location
 * @author  <NAME> <<EMAIL>>
 */
class FilesystemLocation implements LocationInterface
{

    /**
     * Path that this location points to.
     *
     * @since 0.1.0
     *
     * @var string
     */
    protected $path;

    /**
     * Extensions that this location can accept.
     *
     * @since 0.1.0
     *
     * @var array
     */
    protected $extensions;

    /**
     * Instantiate a FilesystemLocation object.
     *
     * @since 0.1.0
     *
     * @param string $path       Path that this location points to.
     * @param array  $extensions Array of extensions that this location can accept.
     */
    public function __construct($path, array $extensions = [])
    {
        $this->path       = $path;
        $this->extensions = array_merge($extensions, ['']);
    }

    /**
     * Get the first URI that matches the given criteria.
     *
     * @since 0.1.0
     *
     * @param array $criteria Criteria to match.
     *
     * @return string|false URI that matches the criteria or false if none found.
     */
    public function getURI(array $criteria)
    {
        foreach ($criteria as $entry) {
            if ($uri = $this->transform($entry, true)) {
                return $uri;
            }
        }

        return false;
    }

    /**
     * Get all URIs that match the given criteria.
     *
     * @since 0.1.1
     *
     * @param array $criteria Criteria to match.
     *
     * @return array URIs that match the criteria or empty array if none found.
     */
    public function getURIs(array $criteria)
    {
        $uris = [];

        foreach ($criteria as $entry) {
            if ($uri = $this->transform($entry, false)) {
                $uris = array_merge($uris, (array)$uri);
            }
        }

        return $uris;
    }

    /**
     * Try to transform the entry into possible URIs.
     *
     * @since 0.1.0
     *
     * @param string $entry     Entry to transform.
     * @param bool   $firstOnly Return the first result only.
     *
     * @return array|string|false If $firstOnly is true, returns a string with the URI of the view, or false if none
     *                            found.
     *                            If $firstOnly is false, returns an array with all matching URIs, or an empty array if
     *                            none found.
     */
    protected function transform($entry, $firstOnly = true)
    {
        $uris = [];
        try {
            foreach ($this->extensions as $extension) {

                $variants = [
                    $entry . $extension,
                    $this->path . DIRECTORY_SEPARATOR . $entry . $extension,
                ];

                foreach ($variants as $uri) {
                    if (is_readable($uri)) {
                        if ($firstOnly) {
                            return $uri;
                        }
                        $uris [] = $uri;
                    }
                }
            }
        } catch (Exception $exception) {
            // Fail silently.
        }

        return $firstOnly ? false : $uris;
    }
}<commit_msg>Refactor calculation of URI variants.<commit_after><?php
/**
 * Bright Nucleus View Component.
 *
 * @package   BrightNucleus\View
 * @author    <NAME> <<EMAIL>>
 * @license   MIT
 * @link      http://www.brightnucleus.com/
 * @copyright 2016 <NAME>, Bright Nucleus
 */

namespace BrightNucleus\View\Location;

use Exception;

/**
 * Class FilesystemLocation.
 *
 * @since   0.1.0
 *
 * @package BrightNucleus\View\Location
 * @author  <NAME> <<EMAIL>>
 */
class FilesystemLocation implements LocationInterface
{

    /**
     * Path that this location points to.
     *
     * @since 0.1.0
     *
     * @var string
     */
    protected $path;

    /**
     * Extensions that this location can accept.
     *
     * @since 0.1.0
     *
     * @var array
     */
    protected $extensions;

    /**
     * Instantiate a FilesystemLocation object.
     *
     * @since 0.1.0
     *
     * @param string $path       Path that this location points to.
     * @param array  $extensions Array of extensions that this location can accept.
     */
    public function __construct($path, array $extensions = [])
    {
        $this->path       = $path;
        $this->extensions = array_merge($extensions, ['']);
    }

    /**
     * Get the first URI that matches the given criteria.
     *
     * @since 0.1.0
     *
     * @param array $criteria Criteria to match.
     *
     * @return string|false URI that matches the criteria or false if none found.
     */
    public function getURI(array $criteria)
    {
        foreach ($criteria as $entry) {
            if ($uri = $this->transform($entry, true)) {
                return $uri;
            }
        }

        return false;
    }

    /**
     * Get all URIs that match the given criteria.
     *
     * @since 0.1.1
     *
     * @param array $criteria Criteria to match.
     *
     * @return array URIs that match the criteria or empty array if none found.
     */
    public function getURIs(array $criteria)
    {
        $uris = [];

        foreach ($criteria as $entry) {
            if ($uri = $this->transform($entry, false)) {
                $uris = array_merge($uris, (array)$uri);
            }
        }

        return $uris;
    }

    /**
     * Try to transform the entry into possible URIs.
     *
     * @since 0.1.0
     *
     * @param string $entry     Entry to transform.
     * @param bool   $firstOnly Return the first result only.
     *
     * @return array|string|false If $firstOnly is true, returns a string with the URI of the view, or false if none
     *                            found.
     *                            If $firstOnly is false, returns an array with all matching URIs, or an empty array if
     *                            none found.
     */
    protected function transform($entry, $firstOnly = true)
    {
        $uris = [];

        try {
            foreach ($this->getVariants($entry) as $uri) {
                if (is_readable($uri)) {
                    if ($firstOnly) {
                        return $uri;
                    }
                    $uris [] = $uri;
                }
            }
        } catch (Exception $exception) {
            // Fail silently.
        }

        return $firstOnly ? false : $uris;
    }

    /**
     * Get the individual variants that could be matched for the location.
     *
     * @since 0.1.1
     *
     * @param string $entry Entry to get the variants for.
     *
     * @return array Array of variants to check.
     */
    protected function getVariants($entry)
    {
        $variants = [];

        array_map(function ($extension) use ($entry, &$variants) {
            $variants[] = $entry . $extension;
            $variants[] = $this->path . DIRECTORY_SEPARATOR . $entry . $extension;
        }, $this->extensions);

        return $variants;
    }
}
{'fraction_non_alphanumeric': 0.11155160628844839, 'fraction_numerical': 0.007245386192754614, 'mean_word_length': 1.5402777777777779, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2876371', 'n_tokens_mistral': 2141, 'n_tokens_neox': 2038, 'n_words': 618}