Skip to content

Commit

Permalink
Made a few codestyle changes (#36)
Browse files Browse the repository at this point in the history
Code style changes
  • Loading branch information
ping-localhost authored and haroldiedema committed Sep 5, 2018
1 parent f8c18c3 commit 0766e58
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 241 deletions.
7 changes: 3 additions & 4 deletions src/Hostnet/Component/CodeSniffer/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function getSubscribedEvents(): array
/**
* Configuration for standalone use in a system wide installation scenario
*/
public static function configureAsRoot()
public static function configureAsRoot(): void
{
$filesystem = new Filesystem();
$vendor_dir = Path::VENDOR_DIR . '/hostnet/phpcs-tool/src';
Expand Down Expand Up @@ -72,8 +72,7 @@ public function activate(Composer $composer, IOInterface $io): void
public function execute(): void
{
self::configure();

if (!$this->io->isVerbose()) {
if (false === $this->io->isVerbose()) {
return;
}

Expand All @@ -83,7 +82,7 @@ public function execute(): void
/**
* Configure the Hostnet code style
*/
public static function configure()
public static function configure(): void
{
$filesystem = new Filesystem();
$config = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
class AbstractClassMustBePrefixedWithAbstractSniff implements Sniff
{
/**
* @return string[]
* @return int[]
*/
public function register()
public function register(): array
{
return [T_ABSTRACT];
}

/**
* @param File $phpcs_file
* @param int $stack_ptr
* @return void
* {@inheritdoc}
*/
public function process(File $phpcs_file, $stack_ptr)
public function process(File $phpcs_file, $stack_ptr): void
{
// Next should be T_WHITESPACE and then T_CLASS (prevent abstract functions from triggering).
$index = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@
class ClassAndNamespaceMustBeInPascalCaseSniff implements Sniff
{
/**
* @return string[]
* @return int[]
*/
public function register()
public function register(): array
{
return [T_CLASS, T_INTERFACE, T_TRAIT, T_NAMESPACE];
}

/**
* @param File $phpcs_file
* @param int $stack_ptr
* @return void
* {@inheritdoc}
*/
public function process(File $phpcs_file, $stack_ptr)
public function process(File $phpcs_file, $stack_ptr): void
{
$index = 0;
// Find first string (= name).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public function register(): array
return [T_OPEN_TAG];
}

public function process(File $phpcs_file, $stack_ptr)
/**
* {@inheritdoc}
*/
public function process(File $phpcs_file, $stack_ptr): int
{
$tokens = $phpcs_file->getTokens();
$end_of_file = count($tokens) + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,28 @@
class InterfaceMustBePostfixedWithInterfaceSniff implements Sniff
{
/**
* @return string[]
* @return int[]
*/
public function register()
public function register(): array
{
return [T_INTERFACE];
}

/**
* @param File $phpcs_file
* @param int $stack_ptr
* @return void
* {@inheritdoc}
*/
public function process(File $phpcs_file, $stack_ptr)
public function process(File $phpcs_file, $stack_ptr): void
{
// Search till interface name.
$index = 0;
while (isset($phpcs_file->getTokens()[$stack_ptr + $index])
&& $phpcs_file->getTokens()[$stack_ptr + $index]['type'] !== 'T_STRING'
&& $phpcs_file->getTokens()[$stack_ptr + $index]['type'] !== 'T_STRING'
) {
$index++;
}

$ptr = $stack_ptr + $index;
$f_name = $phpcs_file->getTokens()[$ptr]['content'];
if (preg_match('/Interface$/', $f_name)) {
$ptr = $stack_ptr + $index;
if (preg_match('/Interface$/', $phpcs_file->getTokens()[$ptr]['content'])) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@
class NoVerticalWhitespaceBetweenUseStatementsSniff implements Sniff
{
/**
* Returns the token types that this sniff is interested in.
*
* @return array(int)
* @return int[]
*/
public function register()
public function register(): array
{
return [T_USE];
}

/**
* Processes the tokens that this sniff is interested in.
*
* @param File $phpcs_file The file where the token was found.
* @param int|bool $stack_ptr The position in the stack where the token was found.
* @param File $phpcs_file
* @param int|bool $stack_ptr
*
* @return void
*/
public function process(File $phpcs_file, $stack_ptr)
public function process(File $phpcs_file, $stack_ptr): void
{
// only check for use statements that are before the first class declaration
// classes can have use statements for traits, for which we are not interested in this sniff
Expand Down Expand Up @@ -70,7 +66,7 @@ public function process(File $phpcs_file, $stack_ptr)
}
}

private function checkForNewlineOrComments(File $phpcs_file, $stack_ptr)
private function checkForNewlineOrComments(File $phpcs_file, $stack_ptr): void
{
$tokens = $phpcs_file->getTokens();
if ($tokens[$stack_ptr]['code'] === T_COMMENT) {
Expand Down
14 changes: 4 additions & 10 deletions src/Hostnet/Sniffs/Classes/OnlyOneUseStatementPerLineSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,24 @@
/**
* Checks if their is only one use statement in every line.
* e.g. use SomeSpace, SomeOtherSpace; is not allowed
*
* Also looks for inline comments in the middle of use statements
* e.g. use MySpace\/ * comment * /SubSpace; is valid PHP
*/
class OnlyOneUseStatementPerLineSniff implements Sniff
{
/**
* Returns the token types that this sniff is interested in.
*
* @return int[]
*/
public function register()
public function register(): array
{
return [T_USE];
}

/**
* Processes the tokens that this sniff is interested in.
*
* @param File $phpcs_file The file where the token was found.
* @param int $stack_ptr The position in the stack where the token was found.
*
* @return void
* {@inheritdoc}
*/
public function process(File $phpcs_file, $stack_ptr)
public function process(File $phpcs_file, $stack_ptr): void
{
// only check for use statements that are before the first class declaration
// classes can have use statements for traits, for which we are not interested in this sniff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ class ProtectedPropertiesAreNotAllowedSniff implements Sniff
/**
* @return int[]
*/
public function register()
public function register(): array
{
return [T_PROTECTED];
}

/**
* @param File $phpcs_file
* @param int $stack_ptr
*
* @return void
* {@inheritdoc}
*/
public function process(File $phpcs_file, $stack_ptr)
public function process(File $phpcs_file, $stack_ptr): void
{
$original_stack_ptr = $stack_ptr;

Expand All @@ -39,7 +36,7 @@ public function process(File $phpcs_file, $stack_ptr)

// Skip whitespace and comments
while ($tokens[$stack_ptr]['code'] === T_WHITESPACE
|| $tokens[$stack_ptr]['code'] === T_COMMENT) {
|| $tokens[$stack_ptr]['code'] === T_COMMENT) {
$stack_ptr++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ class TraitMustBePostfixedWithTraitSniff implements Sniff
/**
* @return int[]
*/
public function register()
public function register(): array
{
return [T_TRAIT];
}

/**
* @param File $phpcs_file
* @param int $stack_ptr
*
* @return void
* {@inheritdoc}
*/
public function process(File $phpcs_file, $stack_ptr)
public function process(File $phpcs_file, $stack_ptr): void
{
// Search till trait name.
$index = 0;
Expand All @@ -38,9 +35,8 @@ public function process(File $phpcs_file, $stack_ptr)
$index++;
}

$ptr = $stack_ptr + $index;
$f_name = $phpcs_file->getTokens()[$ptr]['content'];
if (preg_match('/Trait$/', $f_name)) {
$ptr = $stack_ptr + $index;
if (preg_match('/Trait$/', $phpcs_file->getTokens()[$ptr]['content'])) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,22 @@ class UseStatementsAlphabeticallyOrderedSniff implements Sniff
private $current_file;

/**
* Returns the token types that this sniff is interested in.
*
* @return int[]
*/
public function register()
public function register(): array
{
return [T_USE, T_CLASS, T_TRAIT];
}

/**
* Processes the tokens that this sniff is interested in.
*
* @param File $phpcs_file The file where the token was found.
* @param int|bool $stack_ptr The position in the stack where the token was found.
* @param File $phpcs_file
* @param int|bool $stack_ptr
*
* @return void
*/
public function process(File $phpcs_file, $stack_ptr)
public function process(File $phpcs_file, $stack_ptr): void
{
if (in_array($phpcs_file->getTokens()[$stack_ptr]['code'], [T_CLASS, T_TRAIT], true)) {
if (\in_array($phpcs_file->getTokens()[$stack_ptr]['code'], [T_CLASS, T_TRAIT], true)) {
if ($this->initial_use && $this->end_use && $this->fixed) {
$this->fixUseStatements($phpcs_file);
}
Expand Down Expand Up @@ -86,29 +82,23 @@ public function process(File $phpcs_file, $stack_ptr)
$this->fixUseStatements($phpcs_file);
}

private function fixUseStatements(File $phpcs_file)
private function fixUseStatements(File $phpcs_file): void
{
$phpcs_file->fixer->beginChangeset();

usort($this->use_statements, 'strcasecmp');

$content = implode(
"\n",
array_map(
function ($item) {
$extra = ';';
if (isset($this->after_use_statement[$item])) {
$this->after_use_statement[$item] = rtrim($this->after_use_statement[$item], ", \n");
if (!empty($this->after_use_statement[$item])) {
$extra = $this->after_use_statement[$item] . ';';
}
}

return 'use ' . $item . $extra;
},
$this->use_statements
)
);
$content = implode("\n", array_map(function ($item) {
$extra = ';';
if (isset($this->after_use_statement[$item])) {
$this->after_use_statement[$item] = rtrim($this->after_use_statement[$item], ", \n");
if (!empty($this->after_use_statement[$item])) {
$extra = $this->after_use_statement[$item] . ';';
}
}

return 'use ' . $item . $extra;
}, $this->use_statements));

$phpcs_file->fixer->replaceToken($this->initial_use, $content);
for ($i = $this->initial_use + 1; $i < $this->end_use; $i++) {
Expand All @@ -126,12 +116,12 @@ function ($item) {
*
* @return void
*/
private function createAndCheckStatements(File $phpcs_file, $stack_ptr)
private function createAndCheckStatements(File $phpcs_file, $stack_ptr): void
{
$tokens = $phpcs_file->getTokens();
$current_use_stmt = '';

while (!in_array($tokens[$stack_ptr]['code'], [T_WHITESPACE, T_SEMICOLON, T_COMMA], true)) {
while (!\in_array($tokens[$stack_ptr]['code'], [T_WHITESPACE, T_SEMICOLON, T_COMMA], true)) {
if ($tokens[$stack_ptr]['code'] !== T_COMMENT) {
//the expression: use MySpace\/*comment*/SubSpace; is valid PHP
$current_use_stmt .= $tokens[$stack_ptr]['content'];
Expand Down Expand Up @@ -174,7 +164,7 @@ private function createAndCheckStatements(File $phpcs_file, $stack_ptr)
}
}

private function copy(array &$tokens, $initial_ptr, $end_ptr)
private function copy(array &$tokens, $initial_ptr, $end_ptr): string
{
$res = '';
for ($i = $initial_ptr; $i < $end_ptr; $i++) {
Expand Down
Loading

0 comments on commit 0766e58

Please sign in to comment.