Skip to content
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

Added new sniff Generic.PHP.DeclareStrictTypes #1771

394 changes: 394 additions & 0 deletions src/Standards/Generic/Sniffs/PHP/DeclareStrictTypesSniff.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php


/**
* @method string method(bool $b)
*/
class X {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

/**
* @method string method(bool $b)
*/
class X {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
declare(ticks=1) {}

declare(strict_types=1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

declare(strict_types=1);

declare(ticks=1) {}

12 changes: 12 additions & 0 deletions src/Standards/Generic/Tests/PHP/DeclareStrictTypesUnitTest.11.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<title>
<?php
declare(strict_types=1);
echo 'Title';
?>
</title>
</head>

<?php echo $content; ?>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1); ?>
<html>
<head>
<title>
<?php
echo 'Title';
?>
</title>
</head>

<?php echo $content; ?>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
// Comment
declare(strict_types=1); ?>
<html>
<?php echo $content; ?>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
// Comment

declare(strict_types=1);
?>
<html>
<?php echo $content; ?>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
declare(strict_types=1) /* comment */;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

declare(strict_types=1) /* comment */;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
declare(strict_types=1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

declare(strict_types=1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* @copyright 2017
*/


declare(strict_types=1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

/**
* @copyright 2017
*/

declare(strict_types=1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
declare(ticks=1) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

declare(ticks=1) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
declare(strict_types = 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

declare(strict_types=1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1); ?>
<html>
<head>
<title><?php echo 'Title'; ?></title>
</head>

<?php echo $content; ?>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1); ?>
<html>
<head>
<title><?php echo 'Title'; ?></title>
</head>

<?php echo $content; ?>
</html>
13 changes: 13 additions & 0 deletions src/Standards/Generic/Tests/PHP/DeclareStrictTypesUnitTest.7.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


declare(strict_types=1);

?>
<html>
<head>
<title><?php echo 'Title'; ?></title>
</head>

<?php echo $content; ?>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1); ?>
<html>
<head>
<title><?php echo 'Title'; ?></title>
</head>

<?php echo $content; ?>
</html>
15 changes: 15 additions & 0 deletions src/Standards/Generic/Tests/PHP/DeclareStrictTypesUnitTest.8.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* @author
*/

declare(strict_types=1);

?>
<html>
<head>
<title><?php echo 'Title'; ?></title>
</head>

<?php echo $content; ?>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @author
*/

declare(strict_types=1);
?>
<html>
<head>
<title><?php echo 'Title'; ?></title>
</head>

<?php echo $content; ?>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php /* Must be first. */ declare(strict_types=1); /* Comment after. */ ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php /* Must be first. */

declare(strict_types=1);

/* Comment after. */ ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


<?php

declare(strict_types=1);


namespace MyNamespace;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

namespace MyNamespace;
91 changes: 91 additions & 0 deletions src/Standards/Generic/Tests/PHP/DeclareStrictTypesUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Unit test class for the DeclareStrictTypes sniff.
*
* @author Michał Bundyra <contact@webimpress.com>
* @copyright 2006-2017 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Standards\Generic\Tests\PHP;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class DeclareStrictTypesUnitTest extends AbstractSniffUnitTest
{


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList($testFile='')
{
switch ($testFile) {
case 'DeclareStrictTypesUnitTest.1.inc':
return [1 => 1];
case 'DeclareStrictTypesUnitTest.2.inc':
return [2 => 1];
case 'DeclareStrictTypesUnitTest.3.inc':
return [8 => 1];
case 'DeclareStrictTypesUnitTest.4.inc':
return [1 => 1];
case 'DeclareStrictTypesUnitTest.5.inc':
return [2 => 2];
case 'DeclareStrictTypesUnitTest.6.inc':
return [1 => 2];
case 'DeclareStrictTypesUnitTest.7.inc':
return [
1 => 1,
6 => 1,
];
case 'DeclareStrictTypesUnitTest.8.inc':
return [6 => 1];
case 'DeclareStrictTypesUnitTest.9.inc':
return [1 => 2];
case 'DeclareStrictTypesUnitTest.10.inc':
return [
1 => 1,
4 => 1,
];
case 'DeclareStrictTypesUnitTest.11.inc':
return [
1 => 1,
5 => 1,
];
case 'DeclareStrictTypesUnitTest.12.inc':
return [3 => 2];
case 'DeclareStrictTypesUnitTest.13.inc':
return [2 => 2];
}//end switch

return [
1 => 1,
5 => 1,
];

}//end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array<int, int>
*/
public function getWarningList()
{
return [];

}//end getWarningList()


}//end class