Skip to content

Commit

Permalink
Fixed TypeError for DeclareStrictTypesSniff::$spacesCountAroundEquals…
Browse files Browse the repository at this point in the history
…Sign
  • Loading branch information
kukulich committed Mar 20, 2017
1 parent 4a98d9c commit 2b8274d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions SlevomatCodingStandard/Helpers/SniffSettingsHelper.php
Expand Up @@ -5,6 +5,15 @@
class SniffSettingsHelper
{

/**
* @param string|int $settings
* @return int
*/
public static function normalizeInteger($settings): int
{
return (int) trim((string) $settings);
}

/**
* @param mixed[] $settings
* @return mixed[]
Expand Down
Expand Up @@ -2,6 +2,7 @@

namespace SlevomatCodingStandard\Sniffs\TypeHints;

use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;

class DeclareStrictTypesSniff implements \PHP_CodeSniffer_Sniff
Expand Down Expand Up @@ -98,7 +99,8 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $openTagPointer)
}

$strictTypesContent = TokenHelper::getContent($phpcsFile, $strictTypesPointer, $numberPointer);
$format = sprintf('strict_types%1$s=%1$s1', str_repeat(' ', $this->spacesCountAroundEqualsSign));
$spacesCountAroundEqualsSign = SniffSettingsHelper::normalizeInteger($this->spacesCountAroundEqualsSign);
$format = sprintf('strict_types%1$s=%1$s1', str_repeat(' ', $spacesCountAroundEqualsSign));
if ($strictTypesContent !== $format) {
$fix = $phpcsFile->addFixableError(
sprintf(
Expand All @@ -120,7 +122,7 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $openTagPointer)
}

$openingWhitespace = substr($tokens[$openTagPointer]['content'], strlen('<?php'));
$newlinesCountBetweenOpenTagAndDeclare = (int) trim((string) $this->newlinesCountBetweenOpenTagAndDeclare);
$newlinesCountBetweenOpenTagAndDeclare = SniffSettingsHelper::normalizeInteger($this->newlinesCountBetweenOpenTagAndDeclare);
if ($newlinesCountBetweenOpenTagAndDeclare === 0) {
if ($openingWhitespace !== ' ') {
$fix = $phpcsFile->addFixableError(
Expand Down
7 changes: 7 additions & 0 deletions tests/Helpers/SniffSettingsHelperTest.php
Expand Up @@ -5,6 +5,13 @@
class SniffSettingsHelperTest extends \SlevomatCodingStandard\Helpers\TestCase
{

public function testNormalizeInteger()
{
$this->assertSame(2, SniffSettingsHelper::normalizeInteger(2));
$this->assertSame(2, SniffSettingsHelper::normalizeInteger('2'));
$this->assertSame(2, SniffSettingsHelper::normalizeInteger(' 2 '));
}

public function testNormalizeArray()
{
$this->assertSame([
Expand Down

0 comments on commit 2b8274d

Please sign in to comment.