Skip to content

Commit

Permalink
Refactored #39: Apply Symphony2 coding standards
Browse files Browse the repository at this point in the history
 - Applied standards.
  • Loading branch information
andkirby committed Dec 2, 2015
1 parent 795a029 commit 547436a
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 118 deletions.
6 changes: 5 additions & 1 deletion LibHooks/lib/PreCommit/Validator/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ abstract class AbstractValidator
protected $errorMessages = array();

/**
* Init options
*
* Init error collector
*
* @param array $options
* @throws Exception
*/
Expand Down Expand Up @@ -65,7 +69,7 @@ abstract public function validate($content, $file);
* @param int $line
* @return $this
*/
protected function _addError($file, $type, $value = null, $line = null)
protected function addError($file, $type, $value = null, $line = null)
{
$this->errorCollector->addError($file, $type, $this->errorMessages[$type], $value, $line);
}
Expand Down
36 changes: 18 additions & 18 deletions LibHooks/lib/PreCommit/Validator/CodingStandard.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function validateGaps($content, $file)

preg_match_all('/\n\n\n/', $content, $match);
if ($match[0]) {
$this->_addError($file, self::CODE_PHP_GAPS, count($match[0]));
$this->addError($file, self::CODE_PHP_GAPS, count($match[0]));
}

preg_match_all('/(\{|\()\n\n.*|.*\n\n[ ]*(\}|\))/', $content, $match);
Expand All @@ -109,7 +109,7 @@ protected function validateGaps($content, $file)
sort($lines);
//endregion

$this->_addError($file, self::CODE_PHP_BRACKET_GAPS, count($match[0]), $lines);
$this->addError($file, self::CODE_PHP_BRACKET_GAPS, count($match[0]), $lines);
}

return $this;
Expand Down Expand Up @@ -156,22 +156,22 @@ protected function validateCodeStyleByLines($content, $file)
// operator & && must be wrapped with spaces
|| preg_match('/[^\(\s&]&{1,2}|&{1,2}[^\s&$]/i', $str)
) {
$this->_addError($file, self::CODE_PHP_OPERATOR_SPACES_MISSED, $currentString, $line);
$this->addError($file, self::CODE_PHP_OPERATOR_SPACES_MISSED, $currentString, $line);
}

//checking for an assignment in a condition
if (preg_match('~if\s*\((.*?)\)\s*[{:]~s', $str, $a)) {
if (preg_match('~\$[^= ]+ ?= ?[^=]~', $a[1])) {
$this->_addError($file, self::CODE_PHP_CONDITION_ASSIGNMENT, $currentString, $line);
$this->addError($file, self::CODE_PHP_CONDITION_ASSIGNMENT, $currentString, $line);
}
}

if (preg_match('/(?:[,\(\)\{\}=]\s{2,}|\w\s{2,}[\(\)\{\}]|\s+[,]|\S\s+[)]|[(]\s+)/i', $str)) {
$this->_addError($file, self::CODE_PHP_REDUNDANT_SPACES, $currentString, $line);
$this->addError($file, self::CODE_PHP_REDUNDANT_SPACES, $currentString, $line);
}

if (strlen($str) > 120) {
$this->_addError($file, self::CODE_PHP_LINE_EXCEEDS, null, $line);
$this->addError($file, self::CODE_PHP_LINE_EXCEEDS, null, $line);
}

$operators = 'elseif|else if|else|if|switch|foreach|for|while|do';
Expand All @@ -184,36 +184,36 @@ protected function validateCodeStyleByLines($content, $file)
if (preg_match('/^[^A-z0-9\>\$]*(try|do)[^A-z0-9-\$]*$/', trim($str))
&& trim($str) !== $b[1].' {'
) {
$this->_addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
$this->addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
}
} elseif ($b[1] == 'while') {
if (substr(trim($str), -1) == ';' && !preg_match('/^\s+\} while \(.*\);$/', $str)
|| substr(trim($str), -1) != ';' && !preg_match('/^\s+while \(.*\) {$/', $str)
) {
$this->_addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
$this->addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
}
} elseif ($b[1] == 'else') {
if (!preg_match('/\s*} else {$/i', $str)) {
$this->_addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
$this->addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
}
} elseif (substr(trim($b[0]), -3) != ') {') {
$bracketRight = substr_count($b[0], ')');
$bracketLeft = substr_count($b[0], '(');
if ($bracketLeft >= 1 && $bracketLeft == $bracketRight) {
$this->_addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
$this->addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
}
} elseif (substr(ltrim($b[0], ' }'), strlen($b[1]), 1) != ' ') { //check right space after construct
$this->_addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
$this->addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
}
}

//check try..catch
if (preg_match('/[^A-z]try[^A-z]/i', $str) && !preg_match('/^(\s+try \{)$/i', $str, $b)) {
$this->_addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
$this->addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
} elseif (preg_match('/[^A-z]catch/i', $str)
&& !preg_match('/^\s*(\} catch \([A-z0-9_\\]+ \$[A-z0-9_]+\) \{)$/', $str, $m)
) {
$this->_addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
$this->addError($file, self::CODE_PHP_SPACE_BRACKET, $currentString, $line);
}

//check function naming and scope
Expand All @@ -222,17 +222,17 @@ protected function validateCodeStyleByLines($content, $file)
if (preg_match('/^\s*(static )?public /', $str)
&& !preg_match('/public (static )?function ([a-z]{2}|__[a-z]{2})/', $str)
) {
$this->_addError($file, self::CODE_PHP_PUBLIC_METHOD_NAMING_INVALID, $currentString, $line);
$this->addError($file, self::CODE_PHP_PUBLIC_METHOD_NAMING_INVALID, $currentString, $line);
} elseif ($this->useUnderScoreInProtected()
&& preg_match('/^\s*(static )?(protected|private) /', $str)
&& !preg_match('/(protected|private) (static )?function _[a-z]{2}/', $str)
) {
$this->_addError($file, self::CODE_PHP_PROTECTED_METHOD_NAMING_INVALID, $currentString, $line);
$this->addError($file, self::CODE_PHP_PROTECTED_METHOD_NAMING_INVALID, $currentString, $line);
} elseif (!$this->useUnderScoreInProtected()
&& preg_match('/^\s*(static )?(protected|private) /', $str)
&& !preg_match('/(protected|private) (static )?function [a-z]{2}/', $str)
) {
$this->_addError(
$this->addError(
$file,
self::CODE_PHP_PROTECTED_METHOD_NAMING_INVALID_NO_UNDERSCORE,
$currentString,
Expand All @@ -241,7 +241,7 @@ protected function validateCodeStyleByLines($content, $file)
}
}
if (!preg_match('/(protected|private|public) (static )?function/', $str)) {
$this->_addError($file, self::CODE_PHP_METHOD_SCOPE, $currentString, $line);
$this->addError($file, self::CODE_PHP_METHOD_SCOPE, $currentString, $line);
}
}

Expand All @@ -261,7 +261,7 @@ protected function validateCodeStyleByLines($content, $file)
}
if ($vars) {
$values = array('value' => $currentString, 'vars' => implode(',', $vars));
$this->_addError($file, self::CODE_PHP_UNDERSCORE_IN_VAR, $values, $line);
$this->addError($file, self::CODE_PHP_UNDERSCORE_IN_VAR, $values, $line);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion LibHooks/lib/PreCommit/Validator/CodingStandardMagento.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function validate($content, $file)
$currentString = trim($originalArr[$line - 1]);
//check using Mage::throwException();
if (false !== strpos($str, 'Mage::throwException(')) {
$this->_addError($file, self::CODE_PHP_DEPRECATED_THROW_EXCEPTION, $currentString, $line);
$this->addError($file, self::CODE_PHP_DEPRECATED_THROW_EXCEPTION, $currentString, $line);
}
}

Expand Down
10 changes: 5 additions & 5 deletions LibHooks/lib/PreCommit/Validator/CodingStandardPhtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function validateGaps($content, $file)

preg_match_all('/\n\n\n/', $content, $match);
if ($match[0]) {
$this->_addError($file, self::CODE_PHTML_GAPS, count($match[0]));
$this->addError($file, self::CODE_PHTML_GAPS, count($match[0]));
}

return $this;
Expand Down Expand Up @@ -125,7 +125,7 @@ protected function validateStringAlternativeSyntaxUsage($file, $str, $line)
&& substr_count($str, '(') === substr_count($str, ')') //ignore multi-line conditions
&& !preg_match('/[^A-z0-9]+(?:'.$operators.').*?\).*?:/i', $b[0], $m)
) {
$this->_addError($file, self::CODE_PHTML_ALTERNATIVE_SYNTAX, $str, $line);
$this->addError($file, self::CODE_PHTML_ALTERNATIVE_SYNTAX, $str, $line);
}

return $this;
Expand All @@ -150,7 +150,7 @@ protected function validateStringNoUnderscoreInVariableName($file, $str, $line)
}
if ($vars) {
$values = array('value' => $str, 'vars' => implode(',', $vars));
$this->_addError($file, self::CODE_PHTML_UNDERSCORE_IN_VAR, $values, $line);
$this->addError($file, self::CODE_PHTML_UNDERSCORE_IN_VAR, $values, $line);
}
}

Expand All @@ -168,7 +168,7 @@ protected function validateStringNoUnderscoreInVariableName($file, $str, $line)
protected function validateStringNoProtectedMethodUsage($file, $str, $line)
{
if (preg_match('/\$this-\>_[^_]/', $str)) {
$this->_addError($file, self::CODE_PHTML_PROTECTED_METHOD, $str, $line);
$this->addError($file, self::CODE_PHTML_PROTECTED_METHOD, $str, $line);
}

return $this;
Expand All @@ -185,7 +185,7 @@ protected function validateStringNoProtectedMethodUsage($file, $str, $line)
protected function validateStringNoClassesUsage($file, $str, $line)
{
if (preg_match('/[A-z_]{3,}\:\:[A-z_]/', $str)) {
$this->_addError($file, self::CODE_PHTML_CLASS, $str, $line);
$this->addError($file, self::CODE_PHTML_CLASS, $str, $line);
}

return $this;
Expand Down
10 changes: 5 additions & 5 deletions LibHooks/lib/PreCommit/Validator/CommitMsg.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __construct(array $options)
public function validate($message, $file)
{
if (!$this->matchMessage($message)) {
$this->_addError('Commit Message', self::CODE_BAD_COMMIT_MESSAGE, $message->head);
$this->addError('Commit Message', self::CODE_BAD_COMMIT_MESSAGE, $message->head);
}

return !$this->errorCollector->hasErrors();
Expand Down Expand Up @@ -158,7 +158,7 @@ protected function getInterpreterResult($message, array $config)
continue;
}
if (!isset($result[$name]) || !$result[$name]) {
$this->_addError('Commit Message', self::CODE_VERB_INCORRECT, $name);
$this->addError('Commit Message', self::CODE_VERB_INCORRECT, $name);

return false;
}
Expand All @@ -172,15 +172,15 @@ protected function getInterpreterResult($message, array $config)
//find verb key
$key = array_search($message->verb, $this->getVerbs());
if (false === $key) {
$this->_addError('Commit Message', self::CODE_VERB_NOT_FOUND, $message->verb);
$this->addError('Commit Message', self::CODE_VERB_NOT_FOUND, $message->verb);

return false;
}

//check allowed verb by issue type
if (!$this->errorCollector->hasErrors()) {
if (!$message->issue->getType()) {
$this->_addError(
$this->addError(
'Commit Message',
self::CODE_ISSUE_TYPE_INCORRECT,
$message->issue->getOriginalType()
Expand All @@ -191,7 +191,7 @@ protected function getInterpreterResult($message, array $config)
//it's cannot be processed if issue type is not valid
$allowed = $this->getAllowedVerbs($message->issue->getType());
if (!isset($allowed[$key]) || !$allowed[$key]) {
$this->_addError('Commit Message', self::CODE_VERB_INCORRECT, $message->verb);
$this->addError('Commit Message', self::CODE_VERB_INCORRECT, $message->verb);

return false;
}
Expand Down
36 changes: 18 additions & 18 deletions LibHooks/lib/PreCommit/Validator/FileFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ public function validate($content, $file)
return false;
}

if ($this->_isFileAllowed($file)) {
if ($this->isFileAllowed($file)) {
//file is allowed to edit, no need to check protection
return !$this->_isFileSkipped($file);
return !$this->isFileSkipped($file);
}

return !$this->_isFileProtected($file) && !$this->_isFileSkipped($file);
return !$this->isFileProtected($file) && !$this->isFileSkipped($file);
}

/**
* Check file from ignore list
*
* @param $file
* @param string $file
* @return bool
*/
protected function _isFileAllowed($file)
protected function isFileAllowed($file)
{
if ($this->_isFileAllowedByPath($file)) {
if ($this->isFileAllowedByPath($file)) {
return true;
}
$list = Config::getInstance()->getMultiNode(self::XPATH_ALLOW_FILES);
Expand All @@ -97,9 +97,9 @@ protected function _isFileAllowed($file)
* @param string $file
* @return bool
*/
protected function _isFileSkipped($file)
protected function isFileSkipped($file)
{
if ($this->_isFileSkippedByPath($file)) {
if ($this->isFileSkippedByPath($file)) {
return true;
}

Expand All @@ -125,18 +125,18 @@ protected function _isFileSkipped($file)
/**
* Check file from ignore list
*
* @param $file
* @param string $file
* @return bool
*/
protected function _isFileProtected($file)
protected function isFileProtected($file)
{
if ($this->_isFileProtectedByPath($file)) {
if ($this->isFileProtectedByPath($file)) {
return true;
}
$list = Config::getInstance()->getMultiNode(self::XPATH_PROTECT_FILES);
foreach ($list as $item) {
if (strpos($file, $item) === 0) {
$this->_addError($file, self::PROTECTED_FILE, $item);
$this->addError($file, self::PROTECTED_FILE, $item);

return true;
}
Expand All @@ -148,10 +148,10 @@ protected function _isFileProtected($file)
/**
* Check if file in allowed path
*
* @param $file
* @param string $file
* @return bool
*/
protected function _isFileAllowedByPath($file)
protected function isFileAllowedByPath($file)
{
$list = Config::getInstance()->getMultiNode(self::XPATH_ALLOW_PATHS);
foreach ($list as $item) {
Expand All @@ -169,7 +169,7 @@ protected function _isFileAllowedByPath($file)
* @param string $file
* @return bool
*/
protected function _isFileSkippedByPath($file)
protected function isFileSkippedByPath($file)
{
$list = Config::getInstance()->getMultiNode(self::XPATH_SKIP_PATHS);
foreach ($list as $item) {
Expand All @@ -184,15 +184,15 @@ protected function _isFileSkippedByPath($file)
/**
* Check file from ignore list
*
* @param $file
* @param string $file
* @return bool
*/
protected function _isFileProtectedByPath($file)
protected function isFileProtectedByPath($file)
{
$list = Config::getInstance()->getMultiNode(self::XPATH_PROTECT_PATHS);
foreach ($list as $item) {
if (strpos($file, (string) $item) === 0) {
$this->_addError($file, self::PROTECTED_PATH, $item);
$this->addError($file, self::PROTECTED_PATH, $item);

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions LibHooks/lib/PreCommit/Validator/FileStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function validateTabIndents($content, $file)
//$line = count(explode("\n", $matches[0]));

preg_match_all('~(\t)~s', $content, $tbMatches);
$this->_addError(
$this->addError(
$file,
self::CODE_TAB_CHAR,
count($tbMatches[0])
Expand All @@ -81,7 +81,7 @@ protected function validateLineBreaks($content, $file)
{
//checking for windows line breaks
if (preg_match_all('~(\r\n)~s', $content, $lnMatches)) {
$this->_addError(
$this->addError(
$file,
self::CODE_WIN_LINE_BREAK,
count($lnMatches[0])
Expand All @@ -101,7 +101,7 @@ protected function validateLineBreaks($content, $file)
protected function validateBom($content, $file)
{
if (substr($content, 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf)) {
$this->_addError($file, self::CODE_FILE_BOM);
$this->addError($file, self::CODE_FILE_BOM);
}

return $this;
Expand Down
Loading

0 comments on commit 547436a

Please sign in to comment.