<commit_before><?php declare(strict_types = 1);

namespace Churn\Assessors\CyclomaticComplexity;

class CyclomaticComplexityAssessor
{
    /**
     * The total cyclomatic complexity score.
     * @var integer.
     */
    protected $score;

    /**
     * Asses the files cyclomatic complexity.
     * @param  string $filePath Path and file name.
     * @return integer
     */
    public function assess(string $filePath): int
    {
        $this->score = 0;

        $contents = $this->getFileContents($filePath);
        $this->countTheMethods($contents);
        $this->countTheIfStatements($contents);
        $this->countTheElseIfStatements($contents);
        $this->countTheWhileLoops($contents);
        $this->countTheForLoops($contents);
        $this->countTheCaseStatements($contents);
        $this->countTheTernaryOperators($contents);
        $this->countTheLogicalAnds($contents);
        $this->countTheLogicalOrs($contents);

        if ($this->score === 0) {
            $this->score = 1;
        }
        return $this->score;
    }

    /**
     * Count how many methods there are.
     * @param  string $contents File contents.
     * @return void
     */
    protected function countTheMethods(string $contents)
    {
        preg_match("/[ ]function[ ]/", $contents, $matches);
        if (isset($matches[0])) {
            $this->score++;
        }
    }

    /**
     * Count how many if statements there are.
     * @param  string $contents File contents.
     * @return void
     */
    protected function countTheIfStatements(string $contents)
    {
        $this->score += $this->howManyPatternMatches("/[ ]if[ ]{0,}\(/", $contents);
    }

    /**
     * Count how many else if statements there are.
     * @param  string $contents File contents.
     * @return void
     */
    protected function countTheElseIfStatements(string $contents)
    {
        $this->score += $this->howManyPatternMatches("/else[ ]{0,}if[ ]{0,}\(/", $contents);<commit_msg>Fixes #148 - better method name<commit_after><?php declare(strict_types = 1);

namespace Churn\Assessors\CyclomaticComplexity;

class CyclomaticComplexityAssessor
{
    /**
     * The total cyclomatic complexity score.
     * @var integer.
     */
    protected $score;

    /**
     * Asses the files cyclomatic complexity.
     * @param  string $filePath Path and file name.
     * @return integer
     */
    public function assess(string $filePath): int
    {
        $this->score = 0;

        $contents = $this->getFileContents($filePath);
        $this->hasAtLeastOneMethod($contents);
        $this->countTheIfStatements($contents);
        $this->countTheElseIfStatements($contents);
        $this->countTheWhileLoops($contents);
        $this->countTheForLoops($contents);
        $this->countTheCaseStatements($contents);
        $this->countTheTernaryOperators($contents);
        $this->countTheLogicalAnds($contents);
        $this->countTheLogicalOrs($contents);

        if ($this->score === 0) {
            $this->score = 1;
        }
        return $this->score;
    }

    /**
     * Does the class have at least one method?
     * @param  string $contents File contents.
     * @return void
     */
    protected function hasAtLeastOneMethod(string $contents)
    {
        preg_match("/[ ]function[ ]/", $contents, $matches);
        if (isset($matches[0])) {
            $this->score++;
        }
    }

    /**
     * Count how many if statements there are.
     * @param  string $contents File contents.
     * @return void
     */
    protected function countTheIfStatements(string $contents)
    {
        $this->score += $this->howManyPatternMatches("/[ ]if[ ]{0,}\(/", $contents);
    }

    /**
     * Count how many else if statements there are.
     * @param  string $contents File contents.
     * @return void
     */
    protected function countTheElseIfStatements(string $contents)
    {
        $this->score += $this->howManyPatternMatches("/else[ ]{0,}if[ ]{0,}\(/", $contents);
{'fraction_non_alphanumeric': 0.13953488372093023, 'fraction_numerical': 0.004751187796949237, 'mean_word_length': 2.4904013961605584, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1463839', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1157, 'n_words': 257}