Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
16 / 16
DbrQueryUsageSniff
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
16 / 16
 register
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 process
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
15 / 15
<?php
/**
 * Report warnings when $dbr->query() is used instead of $dbr->select()
 */
namespace MediaWiki\Sniffs\Usage;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class DbrQueryUsageSniff implements Sniff {
    /**
     * @inheritDoc
     */
    public function register() : array {
        return [ T_OBJECT_OPERATOR ];
    }
    /**
     * @param File $phpcsFile
     * @param int $stackPtr The current token index.
     * @return void
     */
    public function process( File $phpcsFile, $stackPtr ) {
        $tokens = $phpcsFile->getTokens();
        $dbrPtr = $phpcsFile->findPrevious( Tokens::$emptyTokens, $stackPtr - 1, null, true );
        if ( !$dbrPtr
            || $tokens[$dbrPtr]['code'] !== T_VARIABLE
            || $tokens[$dbrPtr]['content'] !== '$dbr'
        ) {
            return;
        }
        $methodPtr = $phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true );
        if ( $methodPtr
            && $tokens[$methodPtr]['code'] === T_STRING
            && $tokens[$methodPtr]['content'] === 'query'
        ) {
            $phpcsFile->addWarning(
                'Call $dbr->select() wrapper instead of $dbr->query()',
                $stackPtr,
                'DbrQueryFound'
            );
        }
    }
}