<commit_before><?php

namespace Shopware\PluginInfo;

use Shopware\PluginInfo\Backend\AutoDetect;
use Shopware\PluginInfo\Backend\BackendInterface;

use JsonSchema;
use Shopware\PluginInfo\Exceptions\ConstraintException;
use Shopware\PluginInfo\Exceptions\ValidatorException;

class PluginInfo
{
    /**
     * @var Backend\BackendInterface
     */
    private $backend;

    /**
     * @var PluginInfoHydrator
     */
    private $hydrator;

    /**
     * @param BackendInterface $backend
     */
    public function __construct(BackendInterface $backend = null)
    {
        if (!$backend) {
            $backend = new AutoDetect();
        }

        $this->backend = $backend;
        $this->hydrator = new PluginInfoHydrator();
    }

    /**
     * Depending on the backend, plugin could be a directory or zip
     *
     * @param string $plugin
     * @return InfoDecorator
     */
    public function get($plugin)
    {
        $pluginJson = $this->backend->getPluginInfo($plugin);

        $this->validate($pluginJson);

        $pluginStruct = $this->hydrator->get($pluginJson);

        return new InfoDecorator($pluginStruct);
    }

    /**
     * validate a given plugin object against the defined json format
     *
     * @param $json
     * @throws Exceptions\ValidatorException
     */
    private function validate($json)
    {
        $retriever = new JsonSchema\Uri\UriRetriever();
        $schema = $retriever->retrieve(
            'file://' . realpath(__DIR__ . '/../res/plugin-info-schema.json')
        );

        $validator = new JsonSchema\Validator();
        $validator->check(json_decode(json_encode($json)), $schema);

        if (!$validator->isValid()) {
            $errors = array_map(function($error) {
                return $error['property'] . ': ' . $error['message'];
            }, $validator->getErrors());<commit_msg>Detect phar archives - they don't need the file:// prefix<commit_after><?php

namespace Shopware\PluginInfo;

use Shopware\PluginInfo\Backend\AutoDetect;
use Shopware\PluginInfo\Backend\BackendInterface;

use JsonSchema;
use Shopware\PluginInfo\Exceptions\ConstraintException;
use Shopware\PluginInfo\Exceptions\ValidatorException;

class PluginInfo
{
    /**
     * @var Backend\BackendInterface
     */
    private $backend;

    /**
     * @var PluginInfoHydrator
     */
    private $hydrator;

    /**
     * @param BackendInterface $backend
     */
    public function __construct(BackendInterface $backend = null)
    {
        if (!$backend) {
            $backend = new AutoDetect();
        }

        $this->backend = $backend;
        $this->hydrator = new PluginInfoHydrator();
    }

    /**
     * Depending on the backend, plugin could be a directory or zip
     *
     * @param string $plugin
     * @return InfoDecorator
     */
    public function get($plugin)
    {
        $pluginJson = $this->backend->getPluginInfo($plugin);

        $this->validate($pluginJson);

        $pluginStruct = $this->hydrator->get($pluginJson);

        return new InfoDecorator($pluginStruct);
    }

    /**
     * validate a given plugin object against the defined json format
     *
     * @param $json
     * @throws Exceptions\ValidatorException
     */
    private function validate($json)
    {
        $retriever = new JsonSchema\Uri\UriRetriever();

        // Detect phar archives - they don't need the file:// prefix
        $prefix = strpos(__DIR__, 'phar://') === 0 ? '' : 'file://';

        $schema = $retriever->retrieve(
            $prefix . __DIR__ . '/../res/plugin-info-schema.json'
        );


        $validator = new JsonSchema\Validator();
        $validator->check(json_decode(json_encode($json)), $schema);

        if (!$validator->isValid()) {
            $errors = array_map(function($error) {
                return $error['property'] . ': ' . $error['message'];
            }, $validator->getErrors());
{'fraction_non_alphanumeric': 0.13246220855751986, 'fraction_numerical': 0.00025621316935690495, 'mean_word_length': 2.442680776014109, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '7086205', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1096, 'n_words': 233}