Skip to content

Commit 35b8bd7

Browse files
committed
Use Exceptions consistently
As far as I can see, Exceptions thrown by sniffs/utility methods when, for instance, a wrong token is passed, should be `RuntimeException`s, while Exceptions thrown by the `Tokenizer` classes should be `TokenizerException`s. In a number of places this was applied inconsistently. This has now been fixed. Aside from that, for quite a number of methods, the `Exception` being thrown has now been documented.
1 parent dc0b81f commit 35b8bd7

File tree

15 files changed

+43
-25
lines changed

15 files changed

+43
-25
lines changed

src/Config.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ public function restoreDefaults()
584584
* @param int $pos The position of the argument on the command line.
585585
*
586586
* @return void
587+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
587588
*/
588589
public function processShortArgument($arg, $pos)
589590
{
@@ -688,6 +689,7 @@ public function processShortArgument($arg, $pos)
688689
* @param int $pos The position of the argument on the command line.
689690
*
690691
* @return void
692+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
691693
*/
692694
public function processLongArgument($arg, $pos)
693695
{
@@ -1249,6 +1251,7 @@ public function processLongArgument($arg, $pos)
12491251
* @param int $pos The position of the argument on the command line.
12501252
*
12511253
* @return void
1254+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
12521255
*/
12531256
public function processUnknownArgument($arg, $pos)
12541257
{
@@ -1274,6 +1277,7 @@ public function processUnknownArgument($arg, $pos)
12741277
* @param string $path The path to the file to add.
12751278
*
12761279
* @return void
1280+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
12771281
*/
12781282
public function processFilePath($path)
12791283
{
@@ -1555,7 +1559,7 @@ public static function getExecutablePath($name)
15551559
*
15561560
* @return bool
15571561
* @see getConfigData()
1558-
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the config file can not be written.
1562+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the config file can not be written.
15591563
*/
15601564
public static function setConfigData($key, $value, $temp=false)
15611565
{
@@ -1636,6 +1640,7 @@ public static function setConfigData($key, $value, $temp=false)
16361640
*
16371641
* @return array<string, string>
16381642
* @see getConfigData()
1643+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the config file could not be read.
16391644
*/
16401645
public static function getAllConfigData()
16411646
{

src/Files/File.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,15 +1291,15 @@ public function getDeclarationName($stackPtr)
12911291
* to acquire the parameters for.
12921292
*
12931293
* @return array
1294-
* @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of
1295-
* type T_FUNCTION or T_CLOSURE.
1294+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified $stackPtr is not of
1295+
* type T_FUNCTION or T_CLOSURE.
12961296
*/
12971297
public function getMethodParameters($stackPtr)
12981298
{
12991299
if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
13001300
&& $this->tokens[$stackPtr]['code'] !== T_CLOSURE
13011301
) {
1302-
throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
1302+
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
13031303
}
13041304

13051305
$opener = $this->tokens[$stackPtr]['parenthesis_opener'];
@@ -1483,15 +1483,15 @@ public function getMethodParameters($stackPtr)
14831483
* acquire the properties for.
14841484
*
14851485
* @return array
1486-
* @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
1487-
* T_FUNCTION token.
1486+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
1487+
* T_FUNCTION token.
14881488
*/
14891489
public function getMethodProperties($stackPtr)
14901490
{
14911491
if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
14921492
&& $this->tokens[$stackPtr]['code'] !== T_CLOSURE
14931493
) {
1494-
throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
1494+
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
14951495
}
14961496

14971497
if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) {
@@ -1632,14 +1632,14 @@ public function getMethodProperties($stackPtr)
16321632
* acquire the properties for.
16331633
*
16341634
* @return array
1635-
* @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
1636-
* T_VARIABLE token, or if the position is not
1637-
* a class member variable.
1635+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
1636+
* T_VARIABLE token, or if the position is not
1637+
* a class member variable.
16381638
*/
16391639
public function getMemberProperties($stackPtr)
16401640
{
16411641
if ($this->tokens[$stackPtr]['code'] !== T_VARIABLE) {
1642-
throw new TokenizerException('$stackPtr must be of type T_VARIABLE');
1642+
throw new RuntimeException('$stackPtr must be of type T_VARIABLE');
16431643
}
16441644

16451645
$conditions = array_keys($this->tokens[$stackPtr]['conditions']);
@@ -1664,7 +1664,7 @@ public function getMemberProperties($stackPtr)
16641664
return [];
16651665
}
16661666
} else {
1667-
throw new TokenizerException('$stackPtr is not a class member var');
1667+
throw new RuntimeException('$stackPtr is not a class member var');
16681668
}
16691669
}
16701670

@@ -1676,7 +1676,7 @@ public function getMemberProperties($stackPtr)
16761676
&& isset($this->tokens[$deepestOpen]['parenthesis_owner']) === true
16771677
&& $this->tokens[$this->tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION
16781678
) {
1679-
throw new TokenizerException('$stackPtr is not a class member var');
1679+
throw new RuntimeException('$stackPtr is not a class member var');
16801680
}
16811681
}
16821682

@@ -1751,13 +1751,13 @@ public function getMemberProperties($stackPtr)
17511751
* acquire the properties for.
17521752
*
17531753
* @return array
1754-
* @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
1755-
* T_CLASS token.
1754+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
1755+
* T_CLASS token.
17561756
*/
17571757
public function getClassProperties($stackPtr)
17581758
{
17591759
if ($this->tokens[$stackPtr]['code'] !== T_CLASS) {
1760-
throw new TokenizerException('$stackPtr must be of type T_CLASS');
1760+
throw new RuntimeException('$stackPtr must be of type T_CLASS');
17611761
}
17621762

17631763
$valid = [
@@ -1938,6 +1938,7 @@ public function isReference($stackPtr)
19381938
* content should be used.
19391939
*
19401940
* @return string The token contents.
1941+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position does not exist.
19411942
*/
19421943
public function getTokensAsString($start, $length, $origContent=false)
19431944
{

src/Files/FileList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function addFile($path, $file=null)
136136
* Get the class name of the filter being used for the run.
137137
*
138138
* @return string
139+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the specified filter could not be found.
139140
*/
140141
private function getFilterClass()
141142
{

src/Reporter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ class Reporter
9292
* @param \PHP_CodeSniffer\Config $config The config data for the run.
9393
*
9494
* @return void
95-
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If a report is not available.
95+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If a custom report class could not be found.
96+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If a report class is incorrectly set up.
9697
*/
9798
public function __construct(Config $config)
9899
{

src/Reports/Cbf.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Cbf implements Report
3434
* @param int $width Maximum allowed line width.
3535
*
3636
* @return bool
37+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
3738
*/
3839
public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
3940
{

src/Reports/Gitblame.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected function getAuthor($line)
6363
* @param string $filename File to blame.
6464
*
6565
* @return array
66+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
6667
*/
6768
protected function getBlameContent($filename)
6869
{

src/Reports/Hgblame.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ protected function getAuthor($line)
6464
* @param string $filename File to blame.
6565
*
6666
* @return array
67+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
6768
*/
6869
protected function getBlameContent($filename)
6970
{

src/Reports/Svnblame.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected function getAuthor($line)
4949
* @param string $filename File to blame.
5050
*
5151
* @return array
52+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
5253
*/
5354
protected function getBlameContent($filename)
5455
{

src/Ruleset.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class Ruleset
122122
* @param \PHP_CodeSniffer\Config $config The config data for the run.
123123
*
124124
* @return void
125+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If no sniffs were registered.
125126
*/
126127
public function __construct(Config $config)
127128
{
@@ -305,7 +306,8 @@ public function explain()
305306
* is only used for debug output.
306307
*
307308
* @return string[]
308-
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the ruleset path is invalid.
309+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException - If the ruleset path is invalid.
310+
* - If a specified autoload file could not be found.
309311
*/
310312
public function processRuleset($rulesetPath, $depth=0)
311313
{

src/Runner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public function runPHPCBF()
233233
* Exits if the minimum requirements of PHP_CodSniffer are not met.
234234
*
235235
* @return array
236-
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
236+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the requirements are not met.
237237
*/
238238
public function checkRequirements()
239239
{
@@ -255,7 +255,7 @@ public function checkRequirements()
255255
* Init the rulesets and other high-level settings.
256256
*
257257
* @return void
258-
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
258+
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If a referenced standard is not installed.
259259
*/
260260
public function init()
261261
{

0 commit comments

Comments
 (0)