<commit_before><?php
namespace App\Lib;

use Cake\Datasource\ConnectionManager;
use Cake\Filesystem\File;
use Cake\Log\Log;
use Cake\Utility\Hash;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Exception\ParseException;

/**
 * Class library for box agnostic helper functions
 */
class CakeboxUtility
{

    /**
     * Retrieve a specific setting from an Ngninx site configuration file.
     *
     * @param string $file Name of the Nginx site-configuration file.
     * @param string $key Keyname to extract the value for.
     * @return mixed String containing value or false when key lookup fails
     */
    public static function getNginxFileSetting($file, $key)
    {
        $content = file_get_contents("/etc/nginx/sites-available/" . $file);
        preg_match_all("/^\\s++$key\\s++(.*);/m", $content, $matches);
        $lastKey = end($matches);
        $value = end($lastKey);
        if (!empty($value)) {
            return $value;
        }
        return false;
    }

    /**
     * Retrieve the installed version of a Composer package by parsing composer.lock.
     *
     * @param string $path Full path to the directory holding the composer.lock file.
     * @param string $package Name of the package to get the version for.
     * @return mixed String containing value or false when key lookup fails
     */
    public static function getComposerLockVersion($path, $package)
    {
        if (!file_exists("$path/composer.lock")) {
            return false;
        }
        // escape / in package name to not break Xpath query
        $package = str_replace('/', '\/', $package);
        $json = json_decode(file_get_contents("$path/composer.lock"), true);
        return implode(Hash::extract($json, "packages.{n}[name=/$package/].version"));
    }

    /**
     * Get the major version by returning first digit of a given full-version string.
     *
     * @param string $version Full version (e.g. 3.0.1-beta, v4.0.1, 1.0).
     * @return int|bool Integer holding major version, false on fail
     */
    public static function getMajorVersion($version)
    {
        preg_match('/\d/m', $version, $matches);
        if (isset($matches[0])) {
            return $matches[0];
        }
        return false;
    }

    /**
     * Get a (recursive) list of all directories and subdirectories for a given
     * using SPL.
     *
     * @param string $dir Full path to the directory.
     * @return object SplIterator containing directories
     */
    public static function getDirectoriesRecursive($dir)
    {
        return new \RecursiveIteratorIterator(
            new \ParentIterator(new \RecursiveDirectoryIterator($dir)),
            \RecursiveIteratorIterator::SELF_FIRST
        );
    }

    /**
     * Returns the content of a(ny) file as html-escaped string.
     *
     * @param string $file Name of the site file.
     * @return string html-escaped file contents
     */
    public function getFileContent($file)
    {
        if (!file_exists($file)) {
            return false;
        }
        $fh = new File($file);
        return htmlentities($fh->read());
    }

    /**
     * Divide an array of data equally over a given number of columns.
     *
     * @param array $data Array with data.
     * @param int $numColumns Number of parts to chop the data into.
     * @return array Array
     */
    public function columnizeArray($data, $numColumns)
    {
        $n = count($data);
        $perColumn = floor($n / $numColumns);
        $rest = $n % $numColumns;<commit_msg>declare statically called methods as static<commit_after><?php
namespace App\Lib;

use Cake\Datasource\ConnectionManager;
use Cake\Filesystem\File;
use Cake\Log\Log;
use Cake\Utility\Hash;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Exception\ParseException;

/**
 * Class library for box agnostic helper functions
 */
class CakeboxUtility
{

    /**
     * Retrieve a specific setting from an Ngninx site configuration file.
     *
     * @param string $file Name of the Nginx site-configuration file.
     * @param string $key Keyname to extract the value for.
     * @return mixed String containing value or false when key lookup fails
     */
    public static function getNginxFileSetting($file, $key)
    {
        $content = file_get_contents("/etc/nginx/sites-available/" . $file);
        preg_match_all("/^\\s++$key\\s++(.*);/m", $content, $matches);
        $lastKey = end($matches);
        $value = end($lastKey);
        if (!empty($value)) {
            return $value;
        }
        return false;
    }

    /**
     * Retrieve the installed version of a Composer package by parsing composer.lock.
     *
     * @param string $path Full path to the directory holding the composer.lock file.
     * @param string $package Name of the package to get the version for.
     * @return mixed String containing value or false when key lookup fails
     */
    public static function getComposerLockVersion($path, $package)
    {
        if (!file_exists("$path/composer.lock")) {
            return false;
        }
        // escape / in package name to not break Xpath query
        $package = str_replace('/', '\/', $package);
        $json = json_decode(file_get_contents("$path/composer.lock"), true);
        return implode(Hash::extract($json, "packages.{n}[name=/$package/].version"));
    }

    /**
     * Get the major version by returning first digit of a given full-version string.
     *
     * @param string $version Full version (e.g. 3.0.1-beta, v4.0.1, 1.0).
     * @return int|bool Integer holding major version, false on fail
     */
    public static function getMajorVersion($version)
    {
        preg_match('/\d/m', $version, $matches);
        if (isset($matches[0])) {
            return $matches[0];
        }
        return false;
    }

    /**
     * Get a (recursive) list of all directories and subdirectories for a given
     * using SPL.
     *
     * @param string $dir Full path to the directory.
     * @return object SplIterator containing directories
     */
    public static function getDirectoriesRecursive($dir)
    {
        return new \RecursiveIteratorIterator(
            new \ParentIterator(new \RecursiveDirectoryIterator($dir)),
            \RecursiveIteratorIterator::SELF_FIRST
        );
    }

    /**
     * Returns the content of a(ny) file as html-escaped string.
     *
     * @param string $file Name of the site file.
     * @return string html-escaped file contents
     */
    public static function getFileContent($file)
    {
        if (!file_exists($file)) {
            return false;
        }
        $fh = new File($file);
        return htmlentities($fh->read());
    }

    /**
     * Divide an array of data equally over a given number of columns.
     *
     * @param array $data Array with data.
     * @param int $numColumns Number of parts to chop the data into.
     * @return array Array
     */
    public static function columnizeArray($data, $numColumns)
    {
        $n = count($data);
        $perColumn = floor($n / $numColumns);
        $rest = $n % $numColumns;
{'fraction_non_alphanumeric': 0.11326587970770095, 'fraction_numerical': 0.002810567734682406, 'mean_word_length': 2.4020076481835564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '211074', 'n_tokens_mistral': 2046, 'n_tokens_neox': 2003, 'n_words': 640}