Skip to content

Several improvements to PrivatePropertiesUnderscoreSniff #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions Yii2/Sniffs/Properties/PrivatePropertiesUnderscoreSniff.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
<?php

/**
* Class for a sniff to oblige private property name prefixed with underscore
*
* This Sniff check if a underscore is missing before private property name.
* The private property name is identified by the PHP token T_VARIABLE
* The private visibility is identified by the PHP token T_PRIVATE
*/
class Yii2_Sniffs_Properties_PrivatePropertiesUnderscoreSniff implements PHP_CodeSniffer_Sniff
{
public function register()
{
return array(
T_PRIVATE,
);
}
public function register()
{
return [T_PRIVATE];
}

public function process(PHP_CodeSniffer_File $file, $pointer)
{
$tokens = $file->getTokens();
if ($tokens[$pointer]['content'] === 'private' &&
$tokens[$pointer + 1]['type'] === 'T_WHITESPACE' &&
$tokens[$pointer + 2]['type'] === 'T_VARIABLE' &&
strpos($tokens[$pointer + 2]['content'], '$_') !== 0) {
$file->addError('Private property name must be prefixed with underscore.', $pointer);
}
}
public function process(PHP_CodeSniffer_File $file, $pointer)
{
$tokens = $file->getTokens();
if ($tokens[$pointer]['content'] === 'private' &&
$tokens[$pointer + 1]['type'] === 'T_WHITESPACE' &&
$tokens[$pointer + 2]['type'] === 'T_VARIABLE' &&
strpos($tokens[$pointer + 2]['content'], '$_') !== 0) {

$data = [$tokens[$pointer + 2]['content']];
$file->addError('Private property name "%s" must be prefixed with underscore.', $pointer, 'NoUnderscore', $data);
}
}
}