<commit_before><?php declare(strict_types=1);
namespace Phan\Analysis;

use Phan\CLI;
use Phan\CodeBase;
use Phan\CodeBase\ClassMap;
use Phan\Config;
use Phan\Issue;
use Phan\Language\Element\AddressableElement;
use Phan\Language\Element\ClassConstant;
use Phan\Language\Element\ClassElement;
use Phan\Language\Element\Method;
use Phan\Language\Element\Property;
use Phan\Library\Map;

class ReferenceCountsAnalyzer
{
    /**
     * Take a look at all globally accessible elements and see if
     * we can find any dead code that is never referenced
     *
     * @return void
     */
    public static function analyzeReferenceCounts(CodeBase $code_base)
    {
        // Check to see if dead code detection is enabled. Keep
        // in mind that the results here are just a guess and
        // we can't tell with certainty that anything is
        // definitely unreferenced.
        if (!Config::get()->dead_code_detection) {
            return;
        }

        // Get the count of all known elements
        $total_count = $code_base->totalElementCount();
        $i = 0;

        // Functions
        self::analyzeElementListReferenceCounts(
            $code_base,
            $code_base->getFunctionMap(),
            Issue::UnreferencedMethod,
            $total_count,
            $i
        );

        // Constants
        self::analyzeElementListReferenceCounts(
            $code_base,
            $code_base->getGlobalConstantMap(),
            Issue::UnreferencedConstant,
            $total_count,
            $i
        );

        // Classes
        self::analyzeElementListReferenceCounts(
            $code_base,
            $code_base->getClassMap(),
            Issue::UnreferencedClass,
            $total_count,
            $i
        );

        // Class Maps
        foreach ($code_base->getClassMapMap() as $class_map) {
            self::analyzeClassMapReferenceCounts(
                $code_base,
                $class_map,
                $total_count,
                $i
            );
        }
    }

    /**
     * @param CodeBase $code_base
     * @param ClassMap $class_map
     * @param int $total_count
     * @param int $i
     *
     * @return void
     */
    private static function analyzeClassMapReferenceCounts(
        CodeBase $code_base,
        ClassMap $class_map,
        int $total_count,
        int &$i
    ) {
        // Constants
        self::analyzeElementListReferenceCounts(
            $code_base,
            $class_map->getClassConstantMap(),
            Issue::UnreferencedConstant,
            $total_count,
            $i
        );

        // Properties
        self::analyzeElementListReferenceCounts(
            $code_base,
            $class_map->getPropertyMap(),
            Issue::UnreferencedProperty,
            $total_count,
            $i
        );

        // Methods
        self::analyzeElementListReferenceCounts(
            $code_base,
            $class_map->getMethodMap(),
            Issue::UnreferencedMethod,
            $total_count,
            $i
        );
    }

    /**
     * @param CodeBase $code_base
     * @param Map|array $element_list
     * @param string $issue_type
     * @param int $total_count
     * @param int $i
     *
     * @return void
     */
    private static function analyzeElementListReferenceCounts(
        CodeBase $code_base,
        $element_list,
        string $issue_type,
        int $total_count,
        int &$i
    ) {
        foreach ($element_list as $element) {
            CLI::progress('dead code', (++$i)/$total_count);
            self::analyzeElementReferenceCounts(
                $code_base, $element, $issue_type
            );
        }
    }

    /**
     * Check to see if the given Clazz is a duplicate
     *
     * @return void
     */
    private static function analyzeElementReferenceCounts(
        CodeBase $code_base,
        AddressableElement $element,
        string $issue_type
    ) {

        // Don't worry about internal elements
        if ($element->isPHPInternal()) {
            return;
        }

        // Skip methods that are overrides of other methods
        if ($element instanceof ClassElement) {
            if ($element instanceof ClassConstant) {
                // should not warn about self::class
                if (strcasecmp($element->getName(), 'class') === 0) {
                    return;
                }
            }

            if ($element->getIsOverride()) {
                return;
            }

            $class_fqsen = $element->getClassFQSEN();

            // Don't analyze elements defined in a parent
            // class
            if ($class_fqsen != $element->getDefiningClassFQSEN()) {
                return;
            }

            $defining_class =
                $element->getClass($code_base);

            // Don't analyze elements on interfaces or on
            // abstract classes, as they're uncallable.
            if ($defining_class->isInterface()
                || $defining_class->isAbstract()
                || $defining_class->isTrait()
            ) {
                return;
            }

            // Ignore magic methods
            if ($element instanceof Method) {
                // Doubly nested so that `$element` shows
                // up as Method in Phan.
                if ($element->getIsMagic()) {
                    return;<commit_msg>Fix uncaught exception when checking if a non-existent class element is unused<commit_after><?php declare(strict_types=1);
namespace Phan\Analysis;

use Phan\CLI;
use Phan\CodeBase;
use Phan\CodeBase\ClassMap;
use Phan\Config;
use Phan\Exception\CodeBaseException;
use Phan\Issue;
use Phan\Language\Element\AddressableElement;
use Phan\Language\Element\ClassConstant;
use Phan\Language\Element\ClassElement;
use Phan\Language\Element\Method;
use Phan\Language\Element\Property;
use Phan\Library\Map;

class ReferenceCountsAnalyzer
{
    /**
     * Take a look at all globally accessible elements and see if
     * we can find any dead code that is never referenced
     *
     * @return void
     */
    public static function analyzeReferenceCounts(CodeBase $code_base)
    {
        // Check to see if dead code detection is enabled. Keep
        // in mind that the results here are just a guess and
        // we can't tell with certainty that anything is
        // definitely unreferenced.
        if (!Config::get()->dead_code_detection) {
            return;
        }

        // Get the count of all known elements
        $total_count = $code_base->totalElementCount();
        $i = 0;

        // Functions
        self::analyzeElementListReferenceCounts(
            $code_base,
            $code_base->getFunctionMap(),
            Issue::UnreferencedMethod,
            $total_count,
            $i
        );

        // Constants
        self::analyzeElementListReferenceCounts(
            $code_base,
            $code_base->getGlobalConstantMap(),
            Issue::UnreferencedConstant,
            $total_count,
            $i
        );

        // Classes
        self::analyzeElementListReferenceCounts(
            $code_base,
            $code_base->getClassMap(),
            Issue::UnreferencedClass,
            $total_count,
            $i
        );

        // Class Maps
        foreach ($code_base->getClassMapMap() as $class_map) {
            self::analyzeClassMapReferenceCounts(
                $code_base,
                $class_map,
                $total_count,
                $i
            );
        }
    }

    /**
     * @param CodeBase $code_base
     * @param ClassMap $class_map
     * @param int $total_count
     * @param int $i
     *
     * @return void
     */
    private static function analyzeClassMapReferenceCounts(
        CodeBase $code_base,
        ClassMap $class_map,
        int $total_count,
        int &$i
    ) {
        // Constants
        self::analyzeElementListReferenceCounts(
            $code_base,
            $class_map->getClassConstantMap(),
            Issue::UnreferencedConstant,
            $total_count,
            $i
        );

        // Properties
        self::analyzeElementListReferenceCounts(
            $code_base,
            $class_map->getPropertyMap(),
            Issue::UnreferencedProperty,
            $total_count,
            $i
        );

        // Methods
        self::analyzeElementListReferenceCounts(
            $code_base,
            $class_map->getMethodMap(),
            Issue::UnreferencedMethod,
            $total_count,
            $i
        );
    }

    /**
     * @param CodeBase $code_base
     * @param Map|array $element_list
     * @param string $issue_type
     * @param int $total_count
     * @param int $i
     *
     * @return void
     */
    private static function analyzeElementListReferenceCounts(
        CodeBase $code_base,
        $element_list,
        string $issue_type,
        int $total_count,
        int &$i
    ) {
        foreach ($element_list as $element) {
            CLI::progress('dead code', (++$i)/$total_count);
            self::analyzeElementReferenceCounts(
                $code_base, $element, $issue_type
            );
        }
    }

    /**
     * Check to see if the given Clazz is a duplicate
     *
     * @return void
     */
    private static function analyzeElementReferenceCounts(
        CodeBase $code_base,
        AddressableElement $element,
        string $issue_type
    ) {

        // Don't worry about internal elements
        if ($element->isPHPInternal()) {
            return;
        }

        // Skip methods that are overrides of other methods
        if ($element instanceof ClassElement) {
            if ($element instanceof ClassConstant) {
                // should not warn about self::class
                if (strcasecmp($element->getName(), 'class') === 0) {
                    return;
                }
            }
            if ($element->getIsOverride()) {
                return;
            }

            $class_fqsen = $element->getClassFQSEN();

            // Don't analyze elements defined in a parent
            // class
            try {
                if ($class_fqsen != $element->getDefiningClassFQSEN()) {
                    return;
                }
            } catch (CodeBaseException $e) {
                // No defining class for property/constant/etc.
            }

            $defining_class =
                $element->getClass($code_base);

            // Don't analyze elements on interfaces or on
            // abstract classes, as they're uncallable.
            if ($defining_class->isInterface()
                || $defining_class->isAbstract()
                || $defining_class->isTrait()
            ) {
                return;
            }

            // Ignore magic methods
            if ($element instanceof Method) {
                // Doubly nested so that `$element` shows
                // up as Method in Phan.
                if ($element->getIsMagic()) {
                    return;
{'fraction_non_alphanumeric': 0.09384323298074315, 'fraction_numerical': 0.0005424464334147003, 'mean_word_length': 1.5924537145535504, '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': '5208009', 'n_tokens_mistral': 2985, 'n_tokens_neox': 2874, 'n_words': 689}