Commit 6243cb3c authored by Alex Pott's avatar Alex Pott Committed by Klaus Purer
Browse files

feat(GenderNeutralComment): Add sniff to check for gendered words like he/him...

feat(GenderNeutralComment): Add sniff to check for gendered words like he/him in comments (#3021632 #20)
parent d99e0f44
Loading
Loading
Loading
Loading
+60 −0
Original line number Diff line number Diff line
<?php
/**
 * Parses and verifies comment language.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */

namespace Drupal\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
 * Parses and verifies that comments use gender neutral language.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */
class GenderNeutralCommentSniff implements Sniff
{


    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array
     */
    public function register()
    {
        return array(
                T_COMMENT,
                T_DOC_COMMENT_STRING,
               );

    }//end register()


    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the current token
     *                                               in the stack passed in $tokens.
     *
     * @return void
     */
    public function process(File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();
        if ((bool) preg_match('/(^|\W)(he|her|hers|him|his|she)($|\W)/i', $tokens[$stackPtr]['content']) === true) {
            $phpcsFile->addError('Unnecessarily gendered language in a comment', $stackPtr, 'GenderNeutral');
        }

    }//end process()


}//end class
+34 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Example gendered comments.
 *
 * He did something.
 * She did something.
 * They did something.
 */

// She wrote this comment.
// He wrote this comment.
// S/he wrote this comment.
// They wrote this comment.
// His foot moved.
// Her foot moved.
// Their foot moved.
// He's got a foot.
// She's got a foot.
// Hers is blue.
// His is blue.
/**
 * Test function.
 *
 * He did something.
 * She did something.
 * They did something.
 */
function example() {
  // They looked at him.
  // The sniff only checked comments not code.
  $a = "The regex finds he, her, hers, him, his and she.";
}
+34 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Example gendered comments.
 *
 * He did something.
 * She did something.
 * They did something.
 */

// She wrote this comment.
// He wrote this comment.
// S/he wrote this comment.
// They wrote this comment.
// His foot moved.
// Her foot moved.
// Their foot moved.
// He's got a foot.
// She's got a foot.
// Hers is blue.
// His is blue.
/**
 * Test function.
 *
 * He did something.
 * She did something.
 * They did something.
 */
function example() {
  // They looked at him.
  // The sniff only checked comments not code.
  $a = "The regex finds he, her, hers, him, his and she.";
}
+57 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Sniffs\Commenting;

use Drupal\Test\CoderSniffUnitTest;

class GenderNeutralCommentUnitTest extends CoderSniffUnitTest
{


    /**
     * Returns the lines where errors should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of errors that should occur on that line.
     *
     * @return array(int => int)
     */
    public function getErrorList($testFile = NULL)
    {
        return array(
            7 => 1,
            8 => 1,
            12 => 1,
            13 => 1,
            14 => 1,
            16 => 1,
            17 => 1,
            19 => 1,
            20 => 1,
            21 => 1,
            22 => 1,
            26 => 1,
            27 => 1,
            31 => 1,
           );

    }//end getErrorList()


    /**
     * Returns the lines where warnings should occur.
     *
     * The key of the array should represent the line number and the value
     * should represent the number of warnings that should occur on that line.
     *
     * @return array(int => int)
     */
    public function getWarningList($testFile = NULL)
    {
        return array(
               );

    }//end getWarningList()


}//end class
+2 −1
Original line number Diff line number Diff line
@@ -371,7 +371,8 @@ class BadUnitTest extends CoderSniffUnitTest
                        815 => 1,
                        820 => 1,
                        827 => 1,
                        833 => 2,
                        829 => 1,
                        835 => 2,
                       );
        }
        return array();
Loading