Skip to content

Commit bdaf11b

Browse files
committed
Modernize: add constant visibility
Constant visibility is available in PHP since PHP 7.1. This commit adds the visibility to all class constants currently in the codebase and starts enforcing constant visibility being required. In most cases, the visibility remains the same as before, i.e. `public`, however, there are a couple of exceptions, which could be considered a breaking change: * `PHP_CodeSniffer\Generators\HTML::STYLESHEET` (was `public`, now `private`) - Even though the class is not `final`, the `Config` only allows for requesting a PHPCS native `Generator` class, so the chances of the class being extended are very slim. - On top of that, this was a function local variable until a few months ago, so chances of any external access to the constant being active are also slim based on the timing. * `PHP_CodeSniffer\Util\Timing::MINUTE_IN_MS` and `PHP_CodeSniffer\Util\Timing::SECOND_IN_MS` (were `public`, now `private`) - These properties were introduced about six months ago and only ever intended for use by the class itself. Aside from the above, there are some visibility changes for class constants which are part of the PHPCS native test suite, so not in the public API.
1 parent 8ee5e66 commit bdaf11b

File tree

8 files changed

+23
-20
lines changed

8 files changed

+23
-20
lines changed

phpcs.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,7 @@
157157
<exclude-pattern>src/Standards/Generic/Sniffs/Commenting/TodoSniff\.php</exclude-pattern>
158158
<exclude-pattern>src/Standards/Generic/Tests/Commenting/TodoUnitTest\.php</exclude-pattern>
159159
</rule>
160+
161+
<!-- Require visibility for class constants. -->
162+
<rule ref="PSR12.Properties.ConstantVisibility"/>
160163
</ruleset>

src/Config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,21 @@ class Config
8888
*
8989
* @var string
9090
*/
91-
const VERSION = '4.0.0';
91+
public const VERSION = '4.0.0';
9292

9393
/**
9494
* Package stability; either stable, beta or alpha.
9595
*
9696
* @var string
9797
*/
98-
const STABILITY = 'alpha';
98+
public const STABILITY = 'alpha';
9999

100100
/**
101101
* Default report width when no report width is provided and 'auto' does not yield a valid width.
102102
*
103103
* @var int
104104
*/
105-
const DEFAULT_REPORT_WIDTH = 80;
105+
public const DEFAULT_REPORT_WIDTH = 80;
106106

107107
/**
108108
* Translation table for config settings which can be changed via multiple CLI flags.

src/Generators/HTML.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class HTML extends Generator
2828
*
2929
* @var string
3030
*/
31-
const STYLESHEET = '<style>
31+
private const STYLESHEET = '<style>
3232
body {
3333
background-color: #FFFFFF;
3434
font-size: 14px;

src/Util/Help.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class Help
2929
*
3030
* @var string
3131
*/
32-
const DEFAULT_SHORT_OPTIONS = '-hilnpqvw';
32+
public const DEFAULT_SHORT_OPTIONS = '-hilnpqvw';
3333

3434
/**
3535
* Long options which are available for both the `phpcs` as well as the `phpcbf` command.
@@ -38,7 +38,7 @@ final class Help
3838
*
3939
* @var string Comma-separated list of the option names.
4040
*/
41-
const DEFAULT_LONG_OPTIONS = 'basepath,bootstrap,colors,encoding,error-severity,exclude,extensions,file,file-list,filter,ignore,ignore-annotations,no-colors,parallel,php-ini,report-width,runtime-set,severity,sniffs,standard,stdin-path,tab-width,version,vv,vvv,warning-severity';
41+
public const DEFAULT_LONG_OPTIONS = 'basepath,bootstrap,colors,encoding,error-severity,exclude,extensions,file,file-list,filter,ignore,ignore-annotations,no-colors,parallel,php-ini,report-width,runtime-set,severity,sniffs,standard,stdin-path,tab-width,version,vv,vvv,warning-severity';
4242

4343
/**
4444
* Minimum screen width.
@@ -47,21 +47,21 @@ final class Help
4747
*
4848
* @var integer
4949
*/
50-
const MIN_WIDTH = 60;
50+
public const MIN_WIDTH = 60;
5151

5252
/**
5353
* Indent option lines.
5454
*
5555
* @var string
5656
*/
57-
const INDENT = ' ';
57+
public const INDENT = ' ';
5858

5959
/**
6060
* Gutter spacing for between the option argument info and the option description.
6161
*
6262
* @var string
6363
*/
64-
const GUTTER = ' ';
64+
public const GUTTER = ' ';
6565

6666
/**
6767
* The current PHPCS Configuration.

src/Util/MessageCollector.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,42 @@ final class MessageCollector
3333
*
3434
* @var int
3535
*/
36-
const ERROR = 1;
36+
public const ERROR = 1;
3737

3838
/**
3939
* Indicator for a warning.
4040
*
4141
* @var int
4242
*/
43-
const WARNING = 2;
43+
public const WARNING = 2;
4444

4545
/**
4646
* Indicator for a notice.
4747
*
4848
* @var int
4949
*/
50-
const NOTICE = 4;
50+
public const NOTICE = 4;
5151

5252
/**
5353
* Indicator for a deprecation notice.
5454
*
5555
* @var int
5656
*/
57-
const DEPRECATED = 8;
57+
public const DEPRECATED = 8;
5858

5959
/**
6060
* Indicator for ordering the messages based on severity first, order received second.
6161
*
6262
* @var string
6363
*/
64-
const ORDERBY_SEVERITY = 'severity';
64+
public const ORDERBY_SEVERITY = 'severity';
6565

6666
/**
6767
* Indicator for ordering the messages based on the order in which they were received.
6868
*
6969
* @var string
7070
*/
71-
const ORDERBY_RECEIVED = 'received';
71+
public const ORDERBY_RECEIVED = 'received';
7272

7373
/**
7474
* Collected messages.

src/Util/Timing.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class Timing
1919
*
2020
* @var int
2121
*/
22-
const MINUTE_IN_MS = 60000;
22+
private const MINUTE_IN_MS = 60000;
2323

2424
/**
2525
* Number of milliseconds in a second.
2626
*
2727
* @var int
2828
*/
29-
const SECOND_IN_MS = 1000;
29+
private const SECOND_IN_MS = 1000;
3030

3131
/**
3232
* The start time of the run in microseconds.

tests/Core/Ruleset/AbstractRulesetTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract class AbstractRulesetTestCase extends TestCase
1919
*
2020
* @var string
2121
*/
22-
const RUNTIME_EXCEPTION = 'PHP_CodeSniffer\Exceptions\RuntimeException';
22+
private const RUNTIME_EXCEPTION = 'PHP_CodeSniffer\Exceptions\RuntimeException';
2323

2424

2525
/**

tests/Core/Ruleset/PropertyTypeHandlingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ final class PropertyTypeHandlingTest extends TestCase
2929
*
3030
* @var string
3131
*/
32-
const SNIFF_CODE = 'TestStandard.SetProperty.PropertyTypeHandling';
32+
private const SNIFF_CODE = 'TestStandard.SetProperty.PropertyTypeHandling';
3333

3434
/**
3535
* Class name of the sniff used in these tests.
3636
*
3737
* @var string
3838
*/
39-
const SNIFF_CLASS = 'Fixtures\\TestStandard\\Sniffs\\SetProperty\\PropertyTypeHandlingSniff';
39+
private const SNIFF_CLASS = 'Fixtures\\TestStandard\\Sniffs\\SetProperty\\PropertyTypeHandlingSniff';
4040

4141

4242
/**

0 commit comments

Comments
 (0)