Skip to content

Commit 2b8274d

Browse files
committed
Fixed TypeError for DeclareStrictTypesSniff::$spacesCountAroundEqualsSign
1 parent 4a98d9c commit 2b8274d

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

SlevomatCodingStandard/Helpers/SniffSettingsHelper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
class SniffSettingsHelper
66
{
77

8+
/**
9+
* @param string|int $settings
10+
* @return int
11+
*/
12+
public static function normalizeInteger($settings): int
13+
{
14+
return (int) trim((string) $settings);
15+
}
16+
817
/**
918
* @param mixed[] $settings
1019
* @return mixed[]

SlevomatCodingStandard/Sniffs/TypeHints/DeclareStrictTypesSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SlevomatCodingStandard\Sniffs\TypeHints;
44

5+
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
56
use SlevomatCodingStandard\Helpers\TokenHelper;
67

78
class DeclareStrictTypesSniff implements \PHP_CodeSniffer_Sniff
@@ -98,7 +99,8 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $openTagPointer)
9899
}
99100

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

122124
$openingWhitespace = substr($tokens[$openTagPointer]['content'], strlen('<?php'));
123-
$newlinesCountBetweenOpenTagAndDeclare = (int) trim((string) $this->newlinesCountBetweenOpenTagAndDeclare);
125+
$newlinesCountBetweenOpenTagAndDeclare = SniffSettingsHelper::normalizeInteger($this->newlinesCountBetweenOpenTagAndDeclare);
124126
if ($newlinesCountBetweenOpenTagAndDeclare === 0) {
125127
if ($openingWhitespace !== ' ') {
126128
$fix = $phpcsFile->addFixableError(

tests/Helpers/SniffSettingsHelperTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
class SniffSettingsHelperTest extends \SlevomatCodingStandard\Helpers\TestCase
66
{
77

8+
public function testNormalizeInteger()
9+
{
10+
$this->assertSame(2, SniffSettingsHelper::normalizeInteger(2));
11+
$this->assertSame(2, SniffSettingsHelper::normalizeInteger('2'));
12+
$this->assertSame(2, SniffSettingsHelper::normalizeInteger(' 2 '));
13+
}
14+
815
public function testNormalizeArray()
916
{
1017
$this->assertSame([

0 commit comments

Comments
 (0)