PropertyProtectedMatcher.php 2.17 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
21
use PhpParser\Node\Identifier;
22
23
24
25

/**
 * Find usages of properties which have been made protected and are
 * not called in $this context.
26
 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 */
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
52
            && $node->name instanceof Identifier
53
            && $node->var->name !== 'this'
54
            && array_key_exists($node->name->name, $this->flatMatcherDefinitions)
55
56
57
58
        ) {
            $match = [
                'restFiles' => [],
                'line' => $node->getAttribute('startLine'),
59
                'message' => 'Fetch of property "' . $node->name->name . '"',
60
61
62
                'indicator' => 'weak',
            ];

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