• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sirbrillig / phpcs-variable-analysis / 15118106362

19 May 2025 04:19PM UTC coverage: 93.79%. Remained the same
15118106362

Pull #353

github

jrfnl
Ruleset: update schema URL

PHPCS now offers permalinks for the schema.

Refs:
* PHPCSStandards/PHP_CodeSniffer 1094
* https://github.com/PHPCSStandards/schema.phpcodesniffer.com
Pull Request #353: Ruleset: update schema URL

1903 of 2029 relevant lines covered (93.79%)

138.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.18
/VariableAnalysis/Lib/ScopeManager.php
1
<?php
2

3
namespace VariableAnalysis\Lib;
4

5
use VariableAnalysis\Lib\ScopeInfo;
6
use VariableAnalysis\Lib\Helpers;
7
use PHP_CodeSniffer\Files\File;
8

9
class ScopeManager
10
{
11
        /**
12
         * An associative array of a list of token index pairs which start and end
13
         * scopes and will be used to check for unused variables.
14
         *
15
         * The outer array of scopes is keyed by a string containing the filename.
16
         * The inner array of scopes in keyed by the scope start token index.
17
         *
18
         * @var array<string, array<int, ScopeInfo>>
19
         */
20
        private $scopes = [];
21

22
        /**
23
         * Add a scope's start and end index to our record for the file.
24
         *
25
         * @param File $phpcsFile
26
         * @param int  $scopeStartIndex
27
         *
28
         * @return ScopeInfo
29
         */
30
        public function recordScopeStartAndEnd(File $phpcsFile, $scopeStartIndex)
356✔
31
        {
32
                $scopeEndIndex = Helpers::getScopeCloseForScopeOpen($phpcsFile, $scopeStartIndex);
356✔
33
                $filename = $phpcsFile->getFilename();
356✔
34
                if (! isset($this->scopes[$filename])) {
356✔
35
                        $this->scopes[$filename] = [];
356✔
36
                }
178✔
37
                Helpers::debug('recording scope for file', $filename, 'start/end', $scopeStartIndex, $scopeEndIndex);
356✔
38
                $scope = new ScopeInfo($scopeStartIndex, $scopeEndIndex);
356✔
39
                $this->scopes[$filename][$scopeStartIndex] = $scope;
356✔
40
                return $scope;
356✔
41
        }
42

43
        /**
44
         * Return the scopes for a file.
45
         *
46
         * @param string $filename
47
         *
48
         * @return ScopeInfo[]
49
         */
50
        public function getScopesForFilename($filename)
356✔
51
        {
52
                if (empty($this->scopes[$filename])) {
356✔
53
                        return [];
356✔
54
                }
55
                return array_values($this->scopes[$filename]);
356✔
56
        }
57

58
        /**
59
         * Return the scope for a scope start index.
60
         *
61
         * @param string $filename
62
         * @param int    $scopeStartIndex
63
         *
64
         * @return ScopeInfo|null
65
         */
66
        public function getScopeForScopeStart($filename, $scopeStartIndex)
356✔
67
        {
68
                if (empty($this->scopes[$filename][$scopeStartIndex])) {
356✔
69
                        return null;
×
70
                }
71
                return $this->scopes[$filename][$scopeStartIndex];
356✔
72
        }
73

74
        /**
75
         * Find scopes closed by a scope close index.
76
         *
77
         * @param string $filename
78
         * @param int    $scopeEndIndex
79
         *
80
         * @return ScopeInfo[]
81
         */
82
        public function getScopesForScopeEnd($filename, $scopeEndIndex)
356✔
83
        {
84
                $scopePairsForFile = $this->getScopesForFilename($filename);
356✔
85
                $scopeIndicesThisCloses = array_reduce(
356✔
86
                        $scopePairsForFile,
356✔
87
                        /**
88
                         * @param ScopeInfo[] $found
89
                         * @param ScopeInfo   $scope
90
                         *
91
                         * @return ScopeInfo[]
92
                         */
93
                        function ($found, $scope) use ($scopeEndIndex) {
356✔
94
                                if (! is_int($scope->scopeEndIndex)) {
356✔
95
                                        Helpers::debug('No scope closer found for scope start', $scope->scopeStartIndex);
×
96
                                        return $found;
×
97
                                }
98

99
                                if ($scopeEndIndex === $scope->scopeEndIndex) {
356✔
100
                                        $found[] = $scope;
356✔
101
                                }
178✔
102
                                return $found;
356✔
103
                        },
356✔
104
                        []
356✔
105
                );
356✔
106
                return $scopeIndicesThisCloses;
356✔
107
        }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc