<commit_before><?php
/*
 * @author Anakeen
 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
 * @package FDL
*/

$usage = new ApiUsage();
$usage->setDefinitionText("apply given style - if no style is set then update current style");
$styFilePath = $usage->addOptionalParameter("style", "path to style file");
if (!$styFilePath) {
    /**
     * @var Action $action
     */
    $defautStyle=$action->getParam("STYLE");
    $styFilePath=sprintf("STYLE/%s/%s.sty", $defautStyle,$defautStyle);
}
$verbose = ('yes' === $usage->addOptionalParameter('verbose', 'verbose', array(
    'yes',
    'no'
) , 'no'));
$usage->verify();

class styleManager
{
    
    const CUSTOM_RULES_DIR_NAME = "rules.d";
    const DEFAULT_CSS_PARSER_DEPLOY_CLASS = '\Dcp\Style\dcpCssConcatParser';
    const DEFAULT_CSS_PARSER_RUNTIME_CLASS = null;
    
    protected $verbose = false;
    protected $logIndent = 0;
    protected $styleConfig = array();
    /** @var Action $action */
    protected $action = null;
    
    protected function log($msg)
    {
        if ($this->verbose) {
            print str_repeat("\t", $this->logIndent) . " -- " . $msg . PHP_EOL;
        }
    }
    
    public function __construct(Action $action)
    {
        $this->action = $action;
    }
    
    public function loadStyle($styFilePath)
    {
        
        $styleDefinition = $this->loadStyleDefinition($styFilePath);
        $this->computeStyleColors($styleDefinition);
    }
    
    protected function loadStyleDefinition($styFilePath)
    {
        $this->log("load style definition from $styFilePath");
        if (!is_readable($styFilePath)) {
            if (!file_exists($styFilePath)) {
                $msg = "sty file [$styFilePath] does not exists";
            } else {
                $msg = "sty file [$styFilePath] is not readable";
            }
            throw new \Dcp\Style\Exception("STY0001", $msg);
        }
        /** @noinspection PhpIncludeInspection */
        require $styFilePath;
        
        $styleDefinition = array(
            "sty_desc" => empty($sty_desc) ? array() : $sty_desc,
            "sty_const" => empty($sty_const) ? array() : $sty_const,
            "sty_colors" => empty($sty_colors) ? array() : $sty_colors,
            "sty_local" => empty($sty_local) ? array() : $sty_local,
            "sty_rules" => empty($sty_rules) ? array() : $sty_rules
        );
        
        if (!isset($styleDefinition['sty_desc']) || !isset($styleDefinition['sty_desc']['name'])) {
            throw new \Dcp\Style\Exception("STY0002", "Style definition does not contains style name");
        }
        // init with parent style
        if (!empty($sty_inherit)) {
            $this->log("using parent style file: $sty_inherit");
            $this->logIndent+= 1;
            $parentStyleDefinition = $this->loadStyleDefinition($sty_inherit);
            $styleDefinition = array_replace_recursive($parentStyleDefinition, $styleDefinition);
            $this->logIndent-= 1;
        }
        //load rules (rules.d)
        $customRulesDirPath = dirname($styFilePath) . DIRECTORY_SEPARATOR . self::CUSTOM_RULES_DIR_NAME;
        if (is_dir($customRulesDirPath) && is_readable($customRulesDirPath)) {
            $customRules = $styleDefinition['sty_rules'];
            $customRulesFiles = scandir($customRulesDirPath);
            if (false !== $customRulesFiles) {
                foreach ($customRulesFiles as $customRulesFile) {
                    if ($customRulesFile == '.' || $customRulesFile == '..') continue;
                    $customRules = array_replace_recursive($customRules, $this->loadCustomRulesFromFile($customRulesDirPath . DIRECTORY_SEPARATOR . $customRulesFile));
                }
            }
        }
        
        return $styleDefinition;
    }
    
    protected function loadCustomRulesFromFile($customRulesFilePath)
    {
        
        $this->log("load custom rules from $customRulesFilePath");
        if (!is_readable($customRulesFilePath)) {
            if (!file_exists($customRulesFilePath)) {
                $msg = "$customRulesFilePath does not exists";<commit_msg>correct style rules.d analyze (refs #3860)<commit_after><?php
/*
 * @author Anakeen
 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
 * @package FDL
*/

$usage = new ApiUsage();
$usage->setDefinitionText("apply given style - if no style is set then update current style");
$styFilePath = $usage->addOptionalParameter("style", "path to style file");
if (!$styFilePath) {
    /**
     * @var Action $action
     */
    $defautStyle = $action->getParam("STYLE");
    $styFilePath = sprintf("STYLE/%s/%s.sty", $defautStyle, $defautStyle);
}
$verbose = ('yes' === $usage->addOptionalParameter('verbose', 'verbose', array(
    'yes',
    'no'
) , 'no'));
$usage->verify();

class styleManager
{
    
    const CUSTOM_RULES_DIR_NAME = "rules.d";
    const DEFAULT_CSS_PARSER_DEPLOY_CLASS = '\Dcp\Style\dcpCssConcatParser';
    const DEFAULT_CSS_PARSER_RUNTIME_CLASS = null;
    
    protected $verbose = false;
    protected $logIndent = 0;
    protected $styleConfig = array();
    /** @var Action $action */
    protected $action = null;
    
    protected function log($msg)
    {
        if ($this->verbose) {
            print str_repeat("\t", $this->logIndent) . " -- " . $msg . PHP_EOL;
        }
    }
    
    public function __construct(Action $action)
    {
        $this->action = $action;
    }
    
    public function loadStyle($styFilePath)
    {
        
        $styleDefinition = $this->loadStyleDefinition($styFilePath);
        $this->computeStyleColors($styleDefinition);
    }
    
    protected function loadStyleDefinition($styFilePath)
    {
        $this->log("load style definition from $styFilePath");
        if (!is_readable($styFilePath)) {
            if (!file_exists($styFilePath)) {
                $msg = "sty file [$styFilePath] does not exists";
            } else {
                $msg = "sty file [$styFilePath] is not readable";
            }
            throw new \Dcp\Style\Exception("STY0001", $msg);
        }
        /** @noinspection PhpIncludeInspection */
        require $styFilePath;
        
        $styleDefinition = array(
            "sty_desc" => empty($sty_desc) ? array() : $sty_desc,
            "sty_const" => empty($sty_const) ? array() : $sty_const,
            "sty_colors" => empty($sty_colors) ? array() : $sty_colors,
            "sty_local" => empty($sty_local) ? array() : $sty_local,
            "sty_rules" => empty($sty_rules) ? array() : $sty_rules
        );
        
        if (!isset($styleDefinition['sty_desc']) || !isset($styleDefinition['sty_desc']['name'])) {
            throw new \Dcp\Style\Exception("STY0002", "Style definition does not contains style name");
        }
        // init with parent style
        if (!empty($sty_inherit)) {
            $this->log("using parent style file: $sty_inherit");
            $this->logIndent+= 1;
            $parentStyleDefinition = $this->loadStyleDefinition($sty_inherit);
            $styleDefinition = array_replace_recursive($parentStyleDefinition, $styleDefinition);
            $this->logIndent-= 1;
        }
        //load rules (rules.d)
        $customRulesDirPath = dirname($styFilePath) . DIRECTORY_SEPARATOR . self::CUSTOM_RULES_DIR_NAME;
        if (is_dir($customRulesDirPath) && is_readable($customRulesDirPath)) {
            $customRules = & $styleDefinition['sty_rules'];
            $customRulesFiles = scandir($customRulesDirPath);
            if (false !== $customRulesFiles) {
                foreach ($customRulesFiles as $customRulesFile) {
                    if ($customRulesFile == '.' || $customRulesFile == '..') continue;
                    $customRules = array_replace_recursive($customRules, $this->loadCustomRulesFromFile($customRulesDirPath . DIRECTORY_SEPARATOR . $customRulesFile));
                }
            }
        }
        return $styleDefinition;
    }
    
    protected function loadCustomRulesFromFile($customRulesFilePath)
    {
        
        $this->log("load custom rules from $customRulesFilePath");
        if (!is_readable($customRulesFilePath)) {
            if (!file_exists($customRulesFilePath)) {
                $msg = "$customRulesFilePath does not exists";
{'fraction_non_alphanumeric': 0.13753136575457045, 'fraction_numerical': 0.0035846576651929743, 'mean_word_length': 2.5526315789473686, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1626773', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2387, 'n_words': 481}