<commit_before><?php

/**
 * This file is part of the Arachne
 *
 * Copyright (c) <NAME> (<EMAIL>)
 *
 * For the full copyright and license information, please view the file license.md that was distributed with this source code.
 */

namespace Arachne\Verifier;

use Arachne\Verifier\Exception\InvalidArgumentException;
use Nette\Application\BadRequestException;
use Nette\Application\IPresenterFactory;
use Nette\Application\Request;
use Nette\Application\UI\Presenter;
use Nette\Application\UI\PresenterComponent;
use Nette\Application\UI\PresenterComponentReflection;
use Nette\Object;
use ReflectionClass;
use ReflectionMethod;
use Reflector;

/**
 * @author <NAME>
 */
class Verifier extends Object
{

	/** @var IRuleProvider[] */
	protected $ruleProviders;

	/** @var IRuleHandlerLoader */
	protected $handlerLoader;

	/** @var IPresenterFactory */
	protected $presenterFactory;

	/** @var IRule[][] */
	private $cache;

	public function __construct(array $ruleProviders, IRuleHandlerLoader $handlerLoader, IPresenterFactory $presenterFactory)
	{
		$this->ruleProviders = $ruleProviders;
		$this->handlerLoader = $handlerLoader;
		$this->presenterFactory = $presenterFactory;
	}

	/**
	 * Returns rules that are required for given reflection.
	 * @param ReflectionClass|ReflectionMethod $rules
	 * @return IRule[]
	 */
	public function getRules(Reflector $reflection)
	{
		if (!$reflection instanceof ReflectionMethod && !$reflection instanceof ReflectionClass) {
			throw new InvalidArgumentException('Reflection must be an instance of either ReflectionMethod or ReflectionClass.');
		}

		$key = ($reflection instanceof ReflectionMethod ? $reflection->getDeclaringClass()->getName() . '::' : '') . $reflection->getName();
		if (!isset($this->cache[$key])) {
			$rules = array();
			foreach ($this->ruleProviders as $provider) {
				$rules += $provider->getRules($reflection);
			}
			$this->cache[$key] = $rules;
		}

		return $this->cache[$key];
	}

	/**
	 * Checks whether the given rules are met.
	 * @param ReflectionClass|ReflectionMethod $rules
	 * @param Request $request
	 * @param string $component
	 * @throws BadRequestException
	 */
	public function checkRules(array $rules, Request $request, $component = NULL)
	{
		foreach ($rules as $rule) {
			if (!$rule instanceof IRule) {
				continue;
			}
			$this->handlerLoader
				->getRuleHandler(get_class($rule))
				->checkRule($rule, $request, $component);
		}
	}

	/**
	 * Checks whether all rules of the given reflection are met.
	 * @param ReflectionClass|ReflectionMethod $rules
	 * @param Request $request
	 * @param string $component
	 * @throws BadRequestException
	 */
	public function checkReflection(Reflector $reflection, Request $request, $component = NULL)
	{
		$rules = $this->getRules($reflection);
		$this->checkRules($rules, $request, $component);
	}

	/**
	 * Checks whether it is possible to run the given request.
	 * @param Request $request
	 * @param PresenterComponent $component
	 * @return bool
	 */
	public function isLinkVerified(Request $request, PresenterComponent $component)
	{<commit_msg>Removed skipping of non-rules<commit_after><?php

/**
 * This file is part of the Arachne
 *
 * Copyright (c) <NAME> (<EMAIL>)
 *
 * For the full copyright and license information, please view the file license.md that was distributed with this source code.
 */

namespace Arachne\Verifier;

use Arachne\Verifier\Exception\InvalidArgumentException;
use Nette\Application\BadRequestException;
use Nette\Application\IPresenterFactory;
use Nette\Application\Request;
use Nette\Application\UI\Presenter;
use Nette\Application\UI\PresenterComponent;
use Nette\Application\UI\PresenterComponentReflection;
use Nette\Object;
use ReflectionClass;
use ReflectionMethod;
use Reflector;

/**
 * @author <NAME>
 */
class Verifier extends Object
{

	/** @var IRuleProvider[] */
	protected $ruleProviders;

	/** @var IRuleHandlerLoader */
	protected $handlerLoader;

	/** @var IPresenterFactory */
	protected $presenterFactory;

	/** @var IRule[][] */
	private $cache;

	public function __construct(array $ruleProviders, IRuleHandlerLoader $handlerLoader, IPresenterFactory $presenterFactory)
	{
		$this->ruleProviders = $ruleProviders;
		$this->handlerLoader = $handlerLoader;
		$this->presenterFactory = $presenterFactory;
	}

	/**
	 * Returns rules that are required for given reflection.
	 * @param ReflectionClass|ReflectionMethod $rules
	 * @return IRule[]
	 */
	public function getRules(Reflector $reflection)
	{
		if (!$reflection instanceof ReflectionMethod && !$reflection instanceof ReflectionClass) {
			throw new InvalidArgumentException('Reflection must be an instance of either ReflectionMethod or ReflectionClass.');
		}

		$key = ($reflection instanceof ReflectionMethod ? $reflection->getDeclaringClass()->getName() . '::' : '') . $reflection->getName();
		if (!isset($this->cache[$key])) {
			$rules = array();
			foreach ($this->ruleProviders as $provider) {
				$rules += $provider->getRules($reflection);
			}
			$this->cache[$key] = $rules;
		}

		return $this->cache[$key];
	}

	/**
	 * Checks whether the given rules are met.
	 * @param ReflectionClass|ReflectionMethod $rules
	 * @param Request $request
	 * @param string $component
	 * @throws BadRequestException
	 */
	public function checkRules(array $rules, Request $request, $component = NULL)
	{
		foreach ($rules as $rule) {
			$this->handlerLoader
				->getRuleHandler(get_class($rule))
				->checkRule($rule, $request, $component);
		}
	}

	/**
	 * Checks whether all rules of the given reflection are met.
	 * @param ReflectionClass|ReflectionMethod $rules
	 * @param Request $request
	 * @param string $component
	 * @throws BadRequestException
	 */
	public function checkReflection(Reflector $reflection, Request $request, $component = NULL)
	{
		$rules = $this->getRules($reflection);
		$this->checkRules($rules, $request, $component);
	}

	/**
	 * Checks whether it is possible to run the given request.
	 * @param Request $request
	 * @param PresenterComponent $component
	 * @return bool
	 */
	public function isLinkVerified(Request $request, PresenterComponent $component)
	{
{'fraction_non_alphanumeric': 0.12371805306853328, 'fraction_numerical': 0.0, 'mean_word_length': 4.688888888888889, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, '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': '4464793', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1756, 'n_words': 488}