PropertyProtectedMatcher.php 2.08 KB
Newer Older
1
<?php
2
declare(strict_types = 1);
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace TYPO3\CMS\Install\ExtensionScanner\Php\Matcher;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
20
use PhpParser\Node\Identifier;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

/**
 * Find usages of properties which have been made protected and are
 * not called in $this context.
 */
class PropertyProtectedMatcher extends AbstractCoreMatcher
{
    /**
     * Validate config and prepare flat mach array
     *
     * @param array $matcherDefinitions Incoming main configuration
     */
    public function __construct(array $matcherDefinitions)
    {
        $this->matcherDefinitions = $matcherDefinitions;
        $this->validateMatcherDefinitions();
        $this->initializeFlatMatcherDefinitions();
    }

    /**
     * Called by PhpParser.
     *
     * @param Node $node
     */
    public function enterNode(Node $node)
    {
        if (!$this->isFileIgnored($node)
            && !$this->isLineIgnored($node)
            && $node instanceof PropertyFetch
50
            && $node->name instanceof Identifier
51
            && $node->var->name !== 'this'
52
            && in_array($node->name->name, array_keys($this->flatMatcherDefinitions), true)
53
54
55
56
        ) {
            $match = [
                'restFiles' => [],
                'line' => $node->getAttribute('startLine'),
57
                'message' => 'Fetch of property "' . $node->name->name . '"',
58
59
60
                'indicator' => 'weak',
            ];

61
            foreach ($this->flatMatcherDefinitions[$node->name->name]['candidates'] as $candidate) {
62
63
64
65
66
67
                $match['restFiles'] = array_unique(array_merge($match['restFiles'], $candidate['restFiles']));
            }
            $this->matches[] = $match;
        }
    }
}