<commit_before><?php

namespace PhpProbe;

use PhpProbe\Helper\ProbeHelper;
use PhpProbe\Probe\ProbeInterface;

/**
 * Class Manager
 *
 * @author  <NAME> <<EMAIL>>
 * @package PhpProbe
 */
class Manager
{
    /**
     * @var ProbeInterface[]
     */
    protected $probes = array();

    /**
     * @var int
     */
    protected $failCount = 0;

    /**
     * Constructor
     */
    public function __construct()
    {
    }

    /**
     * Check all probes
     *
     * @return $this
     */
    public function checkAll()
    {
        foreach ($this->probes as $probe) {
            $probe->check();
            if ($probe->hasFailed()) {
                $this->failCount++;
            }
        }
        return $this;
    }

    /**
     * Terminate probing
     *
     * @return void
     */
    public function end()
    {
        if (php_sapi_name() == 'cli') {
            if ($this->failCount > 0) {
                exit(1);
            } else {
                exit(0);
            }
        }
    }

    /**
     * Output result of probes in plain text
     *
     * @param bool $includeSuccess Include success messages in output or not
     * @param bool $httpHeader     Send HTTP headers
     *
     * @return $this
     */
    public function outputText($includeSuccess = false, $httpHeader = true)
    {
        $output = '';
        foreach ($this->probes as $probe) {
            if ($probe->hasFailed()) {
                $output .= "# " . $probe->getName() . " - Failure (" . $probe->getErrorMessage() . ")\n";
            } else {
                if ($includeSuccess === true) {
                    $output .= "# " . $probe->getName() . " - Success\n";
                }
            }
        }

        if ($httpHeader === true) {
            $this->setHttpHeader();
        }

        print $output;

        return $this;
    }

    /**
     * Output result of probes in plain text
     *
     * @param bool $includeSuccess Include success messages in output or not
     * @param bool $httpHeader     Send HTTP headers
     *
     * @return $this
     */
    public function outputHtml($includeSuccess = false, $httpHeader = true)
    {
        $htmlOutput = '<ul>';
        foreach ($this->probes as $probe) {
            if ($probe->hasFailed()) {
                $htmlOutput .= '<li>' . $probe->getName() . ' - Failure (' . $probe->getErrorMessage() . ') </li>';
            } else {
                if ($includeSuccess === true) {
                    $htmlOutput .= '<li>' . $probe->getName() . ' - Success</li>';
                }
            }
        }
        $htmlOutput .= '</ul>';

        if ($httpHeader === true) {
            $this->setHttpHeader();
        }

        print $htmlOutput;

        return $this;
    }

    /**
     * Set HTTP headers according to probes' results
     *
     * @return void
     */
    public function setHttpHeader()
    {
        if (php_sapi_name() != 'cli') {
            if ($this->failCount > 0) {
                header("Cache-Control: no-cache, max-age=0");
                header("HTTP/1.1 503 Service Unavailable", null, 503);
            } else {
                header("Cache-Control: no-cache, max-age=0");
            }
        }
    }

    /**
     * Add a probe to check
     *
     * @param ProbeInterface $probe
     *
     * @return $this
     */
    public function addProbe(ProbeInterface $probe)
    {
        $this->probes[$probe->getHash()] = $probe;
        return $this;
    }

    /**
     * Get all probes
     *
     * @return Probe\ProbeInterface[]
     */
    public function getProbes()
    {
        return $this->probes;
    }

    /**
     * @param string $fileName Config filename
     * @param null   $parsingLibrary
     */
    public function importConfig($fileName, $parsingLibrary = null)
    {
        if (is_null($parsingLibrary)) {
            $parsingLibrary = new \Symfony\Component\Yaml\Yaml;
        }

        $probes = $parsingLibrary::parse($fileName)['probes'];

        foreach ($probes as $probeName => $probe) {
            $className = ProbeHelper::getClassNameFromType($probe['type']);
            if (class_exists($className)) {
                $probe = new $className($probeName, $probe['options']);
                $this->addProbe($probe);
            }
        }
    }
}
<commit_msg>Remove direct access to array property for PHP < 5.4 compatibility<commit_after><?php

namespace PhpProbe;

use PhpProbe\Helper\ProbeHelper;
use PhpProbe\Probe\ProbeInterface;

/**
 * Class Manager
 *
 * @author  <NAME> <<EMAIL>>
 * @package PhpProbe
 */
class Manager
{
    /**
     * @var ProbeInterface[]
     */
    protected $probes = array();

    /**
     * @var int
     */
    protected $failCount = 0;

    /**
     * Constructor
     */
    public function __construct()
    {
    }

    /**
     * Check all probes
     *
     * @return $this
     */
    public function checkAll()
    {
        foreach ($this->probes as $probe) {
            $probe->check();
            if ($probe->hasFailed()) {
                $this->failCount++;
            }
        }
        return $this;
    }

    /**
     * Terminate probing
     *
     * @return void
     */
    public function end()
    {
        if (php_sapi_name() == 'cli') {
            if ($this->failCount > 0) {
                exit(1);
            } else {
                exit(0);
            }
        }
    }

    /**
     * Output result of probes in plain text
     *
     * @param bool $includeSuccess Include success messages in output or not
     * @param bool $httpHeader     Send HTTP headers
     *
     * @return $this
     */
    public function outputText($includeSuccess = false, $httpHeader = true)
    {
        $output = '';
        foreach ($this->probes as $probe) {
            if ($probe->hasFailed()) {
                $output .= "# " . $probe->getName() . " - Failure (" . $probe->getErrorMessage() . ")\n";
            } else {
                if ($includeSuccess === true) {
                    $output .= "# " . $probe->getName() . " - Success\n";
                }
            }
        }

        if ($httpHeader === true) {
            $this->setHttpHeader();
        }

        print $output;

        return $this;
    }

    /**
     * Output result of probes in plain text
     *
     * @param bool $includeSuccess Include success messages in output or not
     * @param bool $httpHeader     Send HTTP headers
     *
     * @return $this
     */
    public function outputHtml($includeSuccess = false, $httpHeader = true)
    {
        $htmlOutput = '<ul>';
        foreach ($this->probes as $probe) {
            if ($probe->hasFailed()) {
                $htmlOutput .= '<li>' . $probe->getName() . ' - Failure (' . $probe->getErrorMessage() . ') </li>';
            } else {
                if ($includeSuccess === true) {
                    $htmlOutput .= '<li>' . $probe->getName() . ' - Success</li>';
                }
            }
        }
        $htmlOutput .= '</ul>';

        if ($httpHeader === true) {
            $this->setHttpHeader();
        }

        print $htmlOutput;

        return $this;
    }

    /**
     * Set HTTP headers according to probes' results
     *
     * @return void
     */
    public function setHttpHeader()
    {
        if (php_sapi_name() != 'cli') {
            if ($this->failCount > 0) {
                header("Cache-Control: no-cache, max-age=0");
                header("HTTP/1.1 503 Service Unavailable", null, 503);
            } else {
                header("Cache-Control: no-cache, max-age=0");
            }
        }
    }

    /**
     * Add a probe to check
     *
     * @param ProbeInterface $probe
     *
     * @return $this
     */
    public function addProbe(ProbeInterface $probe)
    {
        $this->probes[$probe->getHash()] = $probe;
        return $this;
    }

    /**
     * Get all probes
     *
     * @return Probe\ProbeInterface[]
     */
    public function getProbes()
    {
        return $this->probes;
    }

    /**
     * @param string $fileName Config filename
     * @param null   $parsingLibrary
     */
    public function importConfig($fileName, $parsingLibrary = null)
    {
        if (is_null($parsingLibrary)) {
            $parsingLibrary = new \Symfony\Component\Yaml\Yaml;
        }

        $parsedFile = $parsingLibrary::parse($fileName);
        $probes     = $parsedFile['probes'];

        foreach ($probes as $probeName => $probe) {
            $className = ProbeHelper::getClassNameFromType($probe['type']);
            if (class_exists($className)) {
                $probe = new $className($probeName, $probe['options']);
                $this->addProbe($probe);
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.14380581634989695, 'fraction_numerical': 0.0036638424547744447, 'mean_word_length': 1.5122231809030773, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 71, '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': '1172308', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2482, 'n_words': 567}