|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +/** |
| 18 | + * Sniff for various Moodle deprecated functions which uses should be replaced. |
| 19 | + * |
| 20 | + * Note that strictly speaking we don't need to extend the Generic Sniff, |
| 21 | + * just configure it in the ruleset.xml like this, for example: |
| 22 | + * |
| 23 | + * <rule ref="Generic.PHP.DeprecatedFunctions"> |
| 24 | + * <properties> |
| 25 | + * <property name="forbiddenFunctions" type="array"> |
| 26 | + * <element key="xxx" value="yyy"/> |
| 27 | + * </property> |
| 28 | + * </properties> |
| 29 | + * </rule> |
| 30 | + * |
| 31 | + * But we have decided to, instead, extend and keep the functions |
| 32 | + * together with the Sniff. Also, this enables to test the Sniff |
| 33 | + * without having to perform any configuration in the fixture files. |
| 34 | + * (because unit tests DO NOT parse the ruleset.xml details, like |
| 35 | + * properties, excludes... and other info). |
| 36 | + * |
| 37 | + * @package local_codechecker |
| 38 | + * @copyright 2021 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com} |
| 39 | + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 40 | + */ |
| 41 | + |
| 42 | +namespace MoodleCodeSniffer\moodle\Sniffs\PHP; |
| 43 | + |
| 44 | +use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DeprecatedFunctionsSniff as GenericDeprecatedFunctionsSniff; |
| 45 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 46 | +use PHP_CodeSniffer\Files\File; |
| 47 | + |
| 48 | +class DeprecatedFunctionsSniff extends GenericDeprecatedFunctionsSniff { |
| 49 | + |
| 50 | + /** |
| 51 | + * If true, an error will be thrown; otherwise a warning. |
| 52 | + * |
| 53 | + * @var boolean |
| 54 | + */ |
| 55 | + public $error = false; // Consider deprecations just warnings. |
| 56 | + |
| 57 | + /** |
| 58 | + * A list of forbidden functions with their alternatives. |
| 59 | + * |
| 60 | + * The value is NULL if no alternative exists. IE, the |
| 61 | + * function should just not be used. |
| 62 | + * |
| 63 | + * @var array<string, string|null> |
| 64 | + */ |
| 65 | + public $forbiddenFunctions = [ |
| 66 | + // Moodle deprecated functions. |
| 67 | + 'print_error' => 'throw new moodle_exception() (using lang strings only if meant to be shown to final user)', |
| 68 | + // Note that, apart from these Moodle explicitly set functions, also, all the internal PHP functions |
| 69 | + // that are deprecated are detected automatically, {@see Generic\Sniffs\PHP\DeprecatedFunctionsSniff}. |
| 70 | + ]; |
| 71 | + |
| 72 | + /** |
| 73 | + * Generates the error or warning for this sniff. |
| 74 | + * |
| 75 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 76 | + * @param int $stackPtr The position of the forbidden function |
| 77 | + * in the token array. |
| 78 | + * @param string $function The name of the forbidden function. |
| 79 | + * @param string $pattern The pattern used for the match. |
| 80 | + * |
| 81 | + * @return void |
| 82 | + * |
| 83 | + * @todo: This method can be removed once/if this PR accepted: |
| 84 | + * https://github.com/squizlabs/PHP_CodeSniffer/pull/3295 |
| 85 | + */ |
| 86 | + protected function addError($phpcsFile, $stackPtr, $function, $pattern=null) |
| 87 | + { |
| 88 | + $data = [$function]; |
| 89 | + $error = 'Function %s() has been deprecated'; |
| 90 | + $type = 'Deprecated'; |
| 91 | + |
| 92 | + if ($pattern === null) { |
| 93 | + $pattern = strtolower($function); |
| 94 | + } |
| 95 | + |
| 96 | + if ($this->forbiddenFunctions[$pattern] !== null |
| 97 | + && $this->forbiddenFunctions[$pattern] !== 'null' |
| 98 | + ) { |
| 99 | + $type .= 'WithAlternative'; |
| 100 | + $data[] = $this->forbiddenFunctions[$pattern]; |
| 101 | + $error .= '; use %s() instead'; |
| 102 | + } |
| 103 | + |
| 104 | + if ($this->error === true) { |
| 105 | + $phpcsFile->addError($error, $stackPtr, $type, $data); |
| 106 | + } else { |
| 107 | + $phpcsFile->addWarning($error, $stackPtr, $type, $data); |
| 108 | + } |
| 109 | + |
| 110 | + }//end addError() |
| 111 | +} |
0 commit comments