Menu

[r13]: / trunk / PhpCheckstyle / src / PHPCheckstyle.php  Maximize  Restore  History

Download this file

1979 lines (1671 with data), 64.5 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
<?php
/*
* $Id: PHPCheckstyle.php 28215 2005-07-28 02:53:05Z hkodungallur $
*
* Copyright(c) 2004-2005, SpikeSource Inc. All Rights Reserved.
* Licensed under the Open Source License version 2.1
* (See http://www.spikesource.com/license.html)
*/
if (!defined("PHPCHECKSTYLE_HOME_DIR")) {
define("PHPCHECKSTYLE_HOME_DIR", dirname(__FILE__)."/..");
}
require_once PHPCHECKSTYLE_HOME_DIR."/src/CheckStyleConfig.php";
require_once PHPCHECKSTYLE_HOME_DIR."/src/styleErrors.inc.php";
require_once PHPCHECKSTYLE_HOME_DIR."/src/errorLevels.inc.php";
require_once PHPCHECKSTYLE_HOME_DIR."/src/TokenUtils.php";
require_once PHPCHECKSTYLE_HOME_DIR."/src/TokenInfo.php";
require_once PHPCHECKSTYLE_HOME_DIR."/src/reporter/PlainFormatReporter.php";
require_once PHPCHECKSTYLE_HOME_DIR."/src/reporter/XmlFormatReporter.php";
require_once PHPCHECKSTYLE_HOME_DIR."/src/reporter/ConsoleReporter.php";
require_once PHPCHECKSTYLE_HOME_DIR."/src/reporter/XmlNCSSReporter.php";
if (!defined("T_ML_COMMENT")) {
define("T_ML_COMMENT", T_COMMENT);
}
/**
* Main Class. Does most of the parsing and processing
*
* @author Hari Kodungallur <hkodungallur@spikesource.com>
*/
class PHPCheckstyle {
// Variables
/**
* @var TokenUtils
*/
private $tokenizer;
private $validExtensions = array("php", "tpl");
// variables used while processing control structure
private $_csLeftBracket = 0;
private $_fcLeftBracket = 0;
private $inDoWhile = false;
private $token = false;
private $prvsToken = false;
private $lineNumber = 0; // Store the current line number
private $_inControlStatement = false; // We are in a control statement declaration (for, if, while, ...)
private $_inArrayStatement = false; // We are in a array statement
private $_inFunctionStatement = false; // We are in a function statement (declaration)
private $_inFuncCall = false; // We are in a function call
private $_inFunction = false; // We are inside a function
private $_privateFunctions = array(); // The list of private functions in the class
private $_privateFunctionsStartLines = array();
private $_functionParameters = array(); // The list of function parameters
private $_usedFunctions = array(); // The list of functions that are used in the class
private $_variables = array(); // The variables used
private $_inSwitch = false; // We are inside a switch statement
private $_switchLevel = 0;
private $_switchHasDefault = false; // The switch has a default statement
private $_caseHasBreak = false; // The case has a break
private $_caseStartLine = 0;
private $_nbFunctionParameters = 0; // Count the number of parameters of the current function
private $_justAfterFuncStmt = false; // We are just after a control statement (last } )
private $_justAfterControlStmt = false; // We are just after a function statement (last } )
private $_functionStartLine = 0; // Starting line of the current function
private $_switchStartLine = 0; // Starting line of the current switch statement
private $_functionReturns = false; // Does the function return a value ?
private $_functionThrows = false; // Does the function throw an exception ?
private $_functionLevel = 0; // Level of Nesting of the function
private $_isFunctionPrivate = false; // Is the function a private function
private $_inClassStatement = false;
private $_constantDef = false;
private $_currentClassname = null;
private $_currentFilename = null;
private $_currentStatement = false;
private $_docblocNbParams = 0; // Number of @params in the docblock of a function
private $_docblocNbReturns = 0; // Number of @return in the docblock of a function
private $_docblocNbThrows = 0; // Number of @throw in the docblock of a function
private $_levelOfNesting = 0;
private $_branchingStack = array();
private $_cyclomaticComplexity = 0;
// For MVC frameworks
private $_isView = false;
private $_isModel = false;
private $_isController = false;
private $_isClass = false;
/**
* These functions are not tested for naming.
*
* @var array List of the magic methods
* @link http://www.php.net/manual/en/language.oop5.magic.php
*/
private $_specialFunctions = array("__construct", "__destruct", "__call", "__get", "__set", "__isset", "__unset", "__sleep", "__wakeup", "__toString", "__set_state", "__clone", "__autoload");
private $_prohibitedFunctions = array('echo', 'system', "print_r", 'dl',
'exec', 'passthru', 'shell_exec',
'copy', 'delete', 'unlink',
'fwrite');
private $_prohibitedTokens = array('T_BAD_CHARACTER', 'T_DECLARE',
'T_ECHO', 'T_ENDDECLARE', 'T_ENDFOR',
'T_ENDFOREACH', 'T_ENDIF',
'T_ENDSWITCH', 'T_ENDWHILE',
'T_END_HEREDOC', 'T_EXIT',
'T_HALT_COMPILER', 'T_INLINE_HTML',
'T_OLD_FUNCTION',
'T_OPEN_TAG_WITH_ECHO', 'T_PRINT');
private $_deprecatedFunctions = array();
private $_systemVariables = array('$this', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_ENV', '$_SERVER');
// The class used to export the result
private $_reporter;
// The class used to export the count of lines
private $_lineCountReporter;
private $_excludeList = array();
private $_rootSourceDir = "";
private $_config;
// Informations used to count lines of code
private $_ncssTotalClasses = 0;
private $_ncssTotalFunctions = 0;
private $_ncssTotalLinesOfCode = 0;
private $_ncssTotalPhpdoc = 0;
private $_ncssTotalLinesPhpdoc = 0;
private $_ncssTotalSingleComment = 0;
private $_ncssTotalMultiComment = 0;
private $_ncssFileClasses = 0;
private $_ncssFileFunctions = 0;
private $_ncssFileLinesOfCode = 0;
private $_ncssFilePhpdoc = 0;
private $_ncssFileLinesPhpdoc = 0;
private $_ncssFileSingleComment = 0;
private $_ncssFileMultiComment = 0;
/**
* Constructor.
*
* @param $outformat output format "text" or "html".
* Accordingly creates a formatter object
* @param $outfile output file where results are stored.
* Note that in case of "html" format, the output is xml and run.php transforms the xml file into html
* @access public
*/
public function PHPCheckstyle($outformat, $outfile, $linecountfile = null) {
// Initialise the Tokenizer
$this->tokenizer = new TokenUtils();
// Initialise the Reporter
if ($outformat == "text") {
$this->_reporter = new PlainFormatReporter($outfile);
} elseif ($outformat == "html") {
$this->_reporter = new XmlFormatReporter($outfile);
} elseif ($outformat == "console") {
$this->_reporter = new ConsoleReporter();
}
if ($linecountfile != null) {
$this->_lineCountReporter = new XmlNCSSReporter($linecountfile);
}
// Initialise the configuration
$this->_config = new CheckStyleConfig("");
$this->_config->parse();
// Load the list of forbidden functions
$this->_prohibitedFunctions = $this->_config->getTestItems('checkProhibitedFunctions');
// Load the list of forbidden tokens
$this->_prohibitedTokens = $this->_config->getTestItems('checkProhibitedTokens');
// Load the list of deprecated function
$this->_deprecatedFunctions = $this->_config->getTestDeprecations('checkDeprecation');
}
/**
* driver function that call processFile repeatedly for each php
* file that is encountered
*
* @param $src a php file or a directory. in case of directory, it
* searches for all the php/tpl files within the directory
* (recursively) and each of those files are processed
* @param $excludes an array of directories or files that need to be
* excluded from processing
* @return nothing
* @access public
*/
public function processFiles($src, $excludes) {
$this->_rootSourceDir = $src;
$this->_excludeList = $excludes;
$files = $this->_getAllPhpFiles($src, $excludes);
// Start reporting the results
$this->_reporter->start();
// Start counting the lines
if ($this->_lineCountReporter != null) {
$this->_lineCountReporter->start();
}
// Process each file
foreach ($files as $file) {
if (is_array($file)) {
continue;
}
$this->_reporter->currentlyProcessing($file);
$this->_processFile($file);
}
// Stop reporting the results
$this->_reporter->stop();
// Write the count of lines for the complete project
if ($this->_lineCountReporter != null) {
$this->_lineCountReporter->writeTotalCount(count($files), $this->_ncssTotalClasses, $this->_ncssTotalFunctions, $this->_ncssTotalLinesOfCode, $this->_ncssTotalPhpdoc, $this->_ncssTotalLinesPhpdoc, $this->_ncssTotalSingleComment, $this->_ncssTotalMultiComment);
}
// Stop counting the lines
if ($this->_lineCountReporter != null) {
$this->_lineCountReporter->stop();
}
}
/**
* Reset the state of the different flags.
*/
private function _resetValues() {
$this->lineNumber = 1;
// Reset the current attributes
$this->_csLeftBracket = 0;
$this->_fcLeftBracket = 0;
$this->inDoWhile = false;
$this->_inControlStatement = false;
$this->_inArrayStatement = false;
$this->_inFunctionStatement = false;
$this->_inFunction = false;
$this->_privateFunctions = array();
$this->_usedFunctions = array();
$this->_variables = array();
$this->_privateFunctionsStartLines = array();
$this->_inSwitch = false;
$this->_switchLevel = 0;
$this->_caseHasBreak = false;
$this->_caseStartLine = 0;
$this->_inFuncCall = false;
$this->_nbFunctionParameters = 0;
$this->_justAfterFuncStmt = false;
$this->_justAfterControlStmt = false;
$this->_functionStartLine = 0;
$this->_functionReturns = false;
$this->_functionThrows = false;
$this->_isFunctionPrivate = false;
$this->_currentStatement = false;
$this->_inClassStatement = false;
$this->_ncssFileClasses = 0;
$this->_ncssFileFunctions = 0;
$this->_ncssFileLinesOfCode = 0;
$this->_ncssFilePhpdoc = 0;
$this->_ncssFileLinesPhpdoc = 0;
$this->_ncssFileSingleComment = 0;
$this->_ncssFileMultiComment = 0;
$this->_currentClassname = null;
$this->_currentFilename = null;
$this->_docblocNbParams = 0;
$this->_docblocNbReturns = 0;
$this->_docblocNbThrows = 0;
$this->_isView = false;
$this->_isModel = false;
$this->_isController = false;
$this->_isClass = false;
}
/**
* Process one php file
*
* @param $f input file
* @return nothing
* @access private
*/
private function _processFile($f) {
if (DEBUG) {
echo "Processing File : ".$f.PHP_EOL;
}
// Reset the tokenizer
$this->tokenizer->reset();
// Reset the state of the attributes
$this->_resetValues();
// Try to detect the type of file in a MVC framework
if (stripos($f, 'view') !== false || stripos($f, 'layouts') !== false) {
$this->_isView = true;
}
if (stripos($f, 'model') !== false) {
$this->_isModel = true;
}
if (stripos($f, 'controller') !== false) {
$this->_isController = true;
}
if (stripos($f, 'class') !== false) { // simple simple data objects
$this->_isClass = true;
}
$this->_currentFilename = $f;
// Tokenize the file
$this->tokenizer->tokenize($f);
// Go to the first token
$this->_moveToken();
// Run through every token of the file
while ($this->token) {
if (is_array($this->token)) {
// The token is an array
list($tok, $text) = $this->token;
$this->_processToken($text, $tok);
} else if (is_string($this->token)) {
// The token is a String
$text = $this->token;
$this->_processString($text);
}
// Go to the next token
$this->_moveToken();
}
// Test the last token of the file
if ($this->_config->getTest('noFileCloseTag')) {
if ($this->tokenizer->checkProvidedToken($this->prvsToken, T_CLOSE_TAG)) {
// Closing tag is not recommended since PHP 5.0
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_END_FILE_CLOSE_TAG, $this->_config->getTestLevel('noFileCloseTag'));
}
}
// Inner HTML is OK for views but not for other classes (controllers, models, ...)
if ($this->_config->getTest('noFileFinishHTML') && !$this->_isView) {
if ($this->tokenizer->checkProvidedToken($this->prvsToken, T_INLINE_HTML)) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_END_FILE_INLINE_HTML, $this->_config->getTestLevel('noFileFinishHTML'));
}
}
// Check for unused private functions
$this->_checkUnusedPrivateFunctions();
// Check for unused variables
$this->_checkUnusedVariables();
// Write the count of lines for this file
if ($this->_lineCountReporter != null) {
$this->_lineCountReporter->writeFileCount($f, $this->_ncssFileClasses, $this->_ncssFileFunctions, $this->_ncssFileLinesOfCode, $this->_ncssFilePhpdoc, $this->_ncssFileLinesPhpdoc, $this->_ncssFileSingleComment, $this->_ncssFileMultiComment);
}
}
/**
* Go through a directory recursively and get all the
* php (with extension .php and .tpl) files
* Ignores files or subdirectories that are in the _excludeList
*
* @param $src source directory
* @return an array of files that end with .php or .tpl
* @access private
*/
private function _getAllPhpFiles($src, $excludes, $dir = '') {
$files[] = array();
if (!is_dir($src)) {
$files[] = $src;
} else {
$root = opendir($src);
if ($root) {
while ($file = readdir($root)) {
// We ignore the current and parent directory links
if ($file == "." || $file == "..") {
continue;
}
// We ignore the subversion directories
if ($file == ".svn") {
continue;
}
$fullPath = $src."/".$file;
$relPath = substr($fullPath, strlen($src) - strlen($dir) + 1);
if (!in_array($relPath, $excludes)) {
if (is_dir($src."/".$file)) {
$files = array_merge($files, $this->_getAllPhpFiles($src."/".$file, $excludes, $dir.'/'.$file));
} else {
$pathParts = pathinfo($file);
if (array_key_exists('extension', $pathParts)) {
if (in_array($pathParts['extension'], $this->validExtensions)) {
$files[] = $src."/".$file;
}
}
}
}
}
}
}
return $files;
}
/**
* Processes a simple string token.
*
* @param $text the token string
* @return nothing
* @access private
*/
private function _processString($text) {
if (DEBUG) {
echo $this->_levelOfNesting." Line : ".$this->lineNumber." String : ".$text.PHP_EOL;
$this->_dumpStack();
}
switch ($text) {
case "{":
// "{" signifies beginning of a block. We need to look for
// its position when it is a beginning of a control structure
// or a function or class definition.
// Check we have a white space before a curly opening
$this->_checkWhiteSpaceBefore($text);
$stackitem = "";
// if _justAfterFuncStmt is set, the "{" is the beginning of a function definition block
if ($this->_justAfterFuncStmt) {
$this->_processFunctionStart();
$stackitem = "function";
}
// if _justAfterControlStmt is set, the "{" is the beginning of a control structure block
if ($this->_justAfterControlStmt) {
$this->_processControlStructureStart();
$stackitem = $this->_currentStatement;
}
// if _inClassStatement is set then we are just after a class declaration
if ($this->_inClassStatement) {
$this->_inClassStatement = false;
$stackitem = "class";
}
// Check if the block is not empty
$this->_checkEmptyBlock();
$this->_levelOfNesting++;
array_push($this->_branchingStack, $stackitem);
break;
case "}":
// "}" signifies the end of a block
// currently tests whether this token resides on a new line.
// This test is desactivated when in a view
if ($this->_config->getTest('controlCloseCurly') && !($this->_isView)) {
$previousTokenInfo = $this->tokenizer->peekPrvsValidToken();
if ($previousTokenInfo->lineOffset == 0) { // the last token was on the same line
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_END_BLOCK_NEW_LINE, $this->_config->getTestLevel('controlCloseCurly'));
}
}
$this->_levelOfNesting--;
array_pop($this->_branchingStack);
// Test for the end of a switch bloc
if ($this->_inSwitch && $this->_levelOfNesting == $this->_switchLevel) {
$this->_processSwitchStop();
}
// Test for the end of a function
if ($this->_levelOfNesting == $this->_functionLevel && $this->_inFunction) {
$this->_processFunctionStop();
}
break;
case ";":
// ";" -> end of statement
// we only need to make sure that we are not hitting ":"
// before "{" in the case of a control structure, in which
// case we have a control structure is not using the curly
// brackets
if ($this->_justAfterControlStmt) {
$this->_justAfterControlStmt = false;
if ($this->_config->getTest('controlStructNeedCurly')) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_CS_NO_OPEN_CURLY, $this->_config->getTestLevel('controlStructNeedCurly'));
}
}
// ";" should never be preceded by a whitespace
$this->_checkNoWhiteSpaceBefore($text);
$this->_checkEmptyStatement();
break;
case "-":
if (!$this->_inFuncCall) {
$this->_checkWhiteSpaceBefore($text);
}
// We allow some '-' signs to skip the the space afterwards for negative numbers
if (!($this->tokenizer->checkNextToken(T_LNUMBER) || // float number
$this->tokenizer->checkNextToken(T_DNUMBER))) { // integer
$this->_checkWhiteSpaceAfter($text);
}
break;
case "=":
$this->_checkInnerAssignment();
$this->_checkSurroundingWhiteSpace($text);
break;
case "<":
case ">":
case "+":
case ".":
case "*":
case "/":
case "?":
case "==":
case ":":
case "%":
// operators generally will need to be surrounded by whitespaces
$this->_checkSurroundingWhiteSpace($text);
break;
case ",":
$this->_checkNoWhiteSpaceBefore($text);
$this->_checkWhiteSpaceAfter($text);
break;
case "!":
$this->_checkNoWhiteSpaceAfter($text);
break;
case "(":
// the only issue with "(" is generally whether there should be space after it or not
if ($this->_inFuncCall) { // inside a function call
$this->_fcLeftBracket += 1;
} elseif ($this->_inControlStatement || $this->_inFunctionStatement) { // inside a function or control statement
$this->_csLeftBracket += 1;
}
$this->_checkNoWhiteSpaceAfter($text);
break;
case ")":
// again the only issue here the space after/before it
if ($this->_inFuncCall) {
$this->_fcLeftBracket -= 1;
} elseif ($this->_inControlStatement || $this->_inFunctionStatement) {
$this->_csLeftBracket -= 1;
}
if ($this->_fcLeftBracket == 0) {
$this->_inFuncCall = false;
}
if ($this->_csLeftBracket == 0) {
if ($this->_inControlStatement) {
$this->_inControlStatement = false;
$this->_checkNeedBraces();
$this->_justAfterControlStmt = true;
} elseif ($this->_inFunctionStatement) {
$this->_inFunctionStatement = false;
$this->_justAfterFuncStmt = true;
}
}
$this->_checkNoWhiteSpaceBefore($text);
break;
case "&":
// One of the function parameter is passed by reference
if ($this->_config->getTest('avoidPassingReferences')) {
if ($this->_inFunctionStatement) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_PASSING_REFERENCE, $this->_config->getTestLevel('avoidPassingReferences'));
}
}
break;
case "[":
$this->_inArrayStatement = true;
break;
case "]":
$this->_inArrayStatement = false;
break;
default:
break;
}
}
/**
* processes a token that is not a string, that is
* a token that is a array of a token id (key) and
* a token text (value).
* @see http://www.php.net/manual/en/tokens.php
*
* @param $text the text of the token
* @param $tok token id
* @return nothing
* @access private
*/
private function _processToken($text, $tok) {
// Debug
if (DEBUG) {
echo $this->_levelOfNesting;
echo " Line : ".$this->lineNumber;
echo " Token ".$this->tokenizer->getTokenName($tok);
echo " : ".$text.PHP_EOL;
$this->_dumpStack();
}
// Check if the token is in the list of prohibited tokens
if ($this->_config->getTest('checkProhibitedTokens') == 1) {
foreach ($this->_prohibitedTokens as $prohibitedTokens) {
if ($this->tokenizer->getTokenName($tok) == $prohibitedTokens) {
$msg = sprintf(PHPCHECKSTYLE_PROHIBITED_TOKEN, $this->tokenizer->getTokenName($tok));
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('checkProhibitedTokens'));
}
}
}
switch ($tok) {
case T_COMMENT:
case T_ML_COMMENT:
case T_DOC_COMMENT:
$this->_processComment($tok, $text);
break;
// check if shorthand code tags are allowed
case T_OPEN_TAG:
if ($this->_config->getTest('noShortPhpCodeTag')) {
$s = strpos($text, '<?php');
if ($s === false) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_WRONG_OPEN_TAG, $this->_config->getTestLevel('noShortPhpCodeTag'));
}
}
break;
// Beginning of a control statement
case T_DO:
case T_WHILE:
case T_IF:
case T_ELSEIF:
case T_FOR:
case T_FOREACH:
$this->_processControlStatement($text);
$this->_cyclomaticComplexity++;
break;
case T_SWITCH:
$this->_processSwitchStart();
$this->_processControlStatement($text);
$this->_cyclomaticComplexity++;
break;
case T_ELSE:
// We don't increment the cyclomatic complexity for the last else
$this->_processControlStatement($text);
break;
case T_CASE:
$this->_processSwitchCase();
$this->_cyclomaticComplexity++;
break;
case T_DEFAULT:
$this->_processSwitchDefault();
break;
case T_BREAK:
$this->_processSwitchBreak();
break;
case T_TRY:
$this->_processControlStatement($text);
break;
case T_CATCH:
$this->_processControlStatement($text);
break;
case T_WHITESPACE:
{
$this->_checkIndentation($text);
break;
}
case T_INLINE_HTML:
break;
// beginning of a function definition
// check also for existance of docblock
case T_FUNCTION:
$this->_checkDocExists();
$this->_processFunctionStatement();
break;
// beginning of a class
// check also for the existence of a docblock
case T_CLASS:
$this->_checkDocExists();
$this->_processClassStatement();
break;
// operators, generally, need to be surrounded by whitespace
case T_PLUS_EQUAL:
case T_MINUS_EQUAL:
case T_MUL_EQUAL:
case T_DIV_EQUAL:
case T_CONCAT_EQUAL:
case T_MOD_EQUAL:
case T_AND_EQUAL:
case T_OR_EQUAL:
case T_XOR_EQUAL:
case T_SL_EQUAL:
case T_SR_EQUAL:
case T_BOOLEAN_OR:
case T_BOOLEAN_AND:
case T_IS_EQUAL:
case T_IS_NOT_EQUAL:
case T_IS_IDENTICAL:
case T_IS_NOT_IDENTICAL:
case T_IS_SMALLER_OR_EQUAL:
case T_IS_GREATER_OR_EQUAL:
$this->_checkSurroundingWhiteSpace($text);
break;
case T_LOGICAL_AND:
case T_LOGICAL_OR:
if ($this->_config->getTest('useBooleanOperators')) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_USE_BOOLEAN_OPERATORS, $this->_config->getTestLevel('useBooleanOperators'));
}
$this->_checkSurroundingWhiteSpace($text);
break;
// ASSUMPTION:
// that T_STRING followed by "(" is a function call
// Actually, I am not sure how good an assumption this is.
case T_STRING:
// Check whether this is a function call
$this->_processFunctionCall($text);
break;
// found constant definition
case T_CONSTANT_ENCAPSED_STRING:
$this->_checkConstantNaming($text);
// Manage new lines inside string
$subToken = strtok($text, PHP_EOL);
while ($subToken !== false) {
// Increment the lines number (one comment is only one token)
$this->lineNumber++;
$subToken = strtok(PHP_EOL);
}
$this->lineNumber--; // One end of line is already counted
break;
// Constant part of string with variables
case T_ENCAPSED_AND_WHITESPACE:
if ($this->_config->getTest('encapsedVariablesInsideString')) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_VARIABLE_INSIDE_STRING, $this->_config->getTestLevel('encapsedVariablesInsideString'));
}
break;
case T_CURLY_OPEN: // for protected variables within strings "{$var}"
$this->_levelOfNesting++;
array_push($this->_branchingStack, 'curly_open');
break;
case T_DOLLAR_OPEN_CURLY_BRACES: // for extended format "${var}"
$this->_levelOfNesting++;
array_push($this->_branchingStack, 'dollar_curly_open');
break;
case T_NEW_LINE:
$this->_countLinesOfCode();
$this->lineNumber++;
break;
case T_RETURN:
$this->_processReturn();
break;
case T_THROW:
$this->_functionThrows = true;
break;
case T_INC:
case T_DEC:
$this->_checkUnaryOperator();
break;
case T_DOUBLE_ARROW:
$this->_checkSurroundingWhiteSpace($text);
break;
case T_OBJECT_OPERATOR:
$this->_checkNoWhiteSpaceBefore($text);
$this->_checkNoWhiteSpaceAfter($text);
break;
case T_START_HEREDOC:
$this->_checkHeredoc();
break;
case T_VARIABLE:
$this->_processVariable($text);
break;
default:
break;
}
}
/**
* Check if the current line if a line of code and if it's the case increment the count.
*
* This function is called when we meet a T_NEW_LINE token.
*/
private function _countLinesOfCode() {
// We get the previous token (T_WHITESPACE, T_COMMENT, ... ignored);
$previousTokenInfo = $this->tokenizer->peekPrvsValidToken();
// If the previous token is not the new line (empty line), we suppose we have some code
if ($previousTokenInfo != null) {
$previousToken = $previousTokenInfo->token;
if (!$this->tokenizer->checkProvidedToken($previousToken, T_NEW_LINE) && $previousTokenInfo->lineOffset == 0) {
$this->_ncssTotalLinesOfCode++;
$this->_ncssFileLinesOfCode++;
}
}
}
/**
* Checks to see if the constant follows the naming convention
* Constants should only have uppercase letters and underscores
*
* @param $text the string containing the constant. note that the
* string also has the quotes (single or double), so we need
* remove them from the string before testing
*/
private function _checkConstantNaming($text) {
if ($this->_constantDef && $this->_config->getTest('constantNaming')) {
$text = ltrim($text, "\"'");
$text = rtrim($text, "\"'");
$ret = preg_match($this->_config->getTestRegExp('constantNaming'), $text);
if (!$ret) {
$msg = sprintf(PHPCHECKSTYLE_CONSTANT_NAMING, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('constantNaming'));
}
$this->_constantDef = false;
}
}
/**
* Checks to see if the variable follows the naming convention
* Variables should only have letters, start with a lowercase and have no underscore.
*
* @param $text the string containing the variable. note that the
* string also has the quotes (single or double), so we need
* remove them from the string before testing
*/
private function _checkVariableNaming($text) {
if ($this->_config->getTest('variableNaming')) {
$texttoTest = ltrim($text, "\"'"); // remove the quotes
$texttoTest = rtrim($texttoTest, "\"'");
if (strpos($texttoTest, "$") === 0) { // remove the "&"
$texttoTest = substr($texttoTest, 1);
}
$ret = preg_match($this->_config->getTestRegExp('variableNaming'), $texttoTest);
if (!$ret) {
$msg = sprintf(PHPCHECKSTYLE_VARIABLE_NAMING, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('variableNaming'));
}
}
}
/**
* Check the naming of a function.
*
* @param $text the name of the function.
*/
private function _checkFunctionNaming($text) {
if ($this->_config->getTest('functionNaming')) {
$ret = preg_match($this->_config->getTestRegExp('functionNaming'), $text);
if (!$ret) {
$msg = sprintf(PHPCHECKSTYLE_FUNCNAME_NAMING, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('functionNaming'));
}
}
}
/**
* Check the naming of a private function.
*
* @param $text the name of the function.
*/
private function _checkPrivateFunctionNaming($text) {
if ($this->_config->getTest('privateFunctionNaming')) {
$ret = preg_match($this->_config->getTestRegExp('privateFunctionNaming'), $text);
if (!$ret) {
$msg = sprintf(PHPCHECKSTYLE_PRIVATE_FUNCNAME_NAMING, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('privateFunctionNaming'));
}
}
}
/**
* Check the naming of a class.
*
* @param $text the name of the class.
*/
private function _checkClassNaming($text) {
if ($this->_config->getTest('classNaming')) {
$ret = preg_match($this->_config->getTestRegExp('classNaming'), $text);
if (!$ret) {
$msg = sprintf(PHPCHECKSTYLE_CLASSNAME_NAMING, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('classNaming'));
}
}
}
/**
* Check the validity of a function call.
*/
private function _processFunctionCall($text) {
if ($text == "define") {
$this->_constantDef = true;
}
if ($this->tokenizer->checkNextValidTextToken("(")) {
// ASSUMPTION:that T_STRING followed by "(" is a function call
$this->_inFuncCall = true;
// Add the function name to the list of used functions
$this->_usedFunctions[$text] = $text;
// Detect prohibited functions
if ($this->_config->getTest('checkProhibitedFunctions')) {
if (in_array($text, $this->_prohibitedFunctions)) {
$msg = sprintf(PHPCHECKSTYLE_PROHIBITED_FUNCTION, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('checkProhibitedFunctions'));
}
}
// Detect deprecated functions
$this->_checkDeprecation($text);
// Detect an @ before the function call
$this->_checkSilenced($text);
// Detect space after function name
if ($this->_config->getTest('noSpaceAfterFunctionName')) {
if (!$this->tokenizer->checkNextTextToken("(")) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_FUNCNAME_SPACE_AFTER, $this->_config->getTestLevel('noSpaceAfterFunctionName'));
}
}
}
// Optimisation : Avoid using count/sizeof inside a loop
if ($this->_config->getTest('functionInsideLoop')) {
if ((strtolower($text) == 'count' || strtolower($text) == 'sizeof') && $this->_inControlStatement) {
if ($this->_currentStatement == 'do' || $this->_currentStatement == 'while' || $this->_currentStatement == 'for' || $this->_currentStatement == 'foreach') {
$msg = sprintf(PHPCHECKSTYLE_FUNCTION_INSIDE_LOOP, strtolower($text));
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('functionInsideLoop'));
}
}
}
}
/**
* Process a control statement declaration (do/while/for/...).
*/
private function _processControlStatement($csText) {
$csText = strtolower($csText);
$this->_inControlStatement = true;
$this->_currentStatement = $csText;
// first token: if not one whitespace, error
if ($this->_config->getTest('spaceAfterControlStmt')) {
if (!$this->tokenizer->checkNextToken(T_WHITESPACE)) {
$msg = sprintf(PHPCHECKSTYLE_SPACE_AFTER_TOKEN, $csText);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('spaceAfterControlStmt'));
}
}
// for some control structures like "else" and "do",
// there is no statments they will be followed directly by "{"
if ($csText == "else" || $csText == "do" || $csText == "try") {
if ($this->tokenizer->checkNextValidTextToken("{")) {
$this->_inControlStatement = false;
$this->_justAfterControlStmt = true;
}
}
// "else if" is different
if ($csText == "else") {
if ($this->tokenizer->checkNextValidToken(T_IF)) {
// control statement for "else" is done with... new control
// statement "if" is starting
$this->_inControlStatement = false;
}
}
// "else" and "elseif" should start in the same line as "}"
if ($csText == "else" || $csText == "elseif") {
$position = $this->_config->getTestProperty('controlStructElse', 'position');
$previousTokenInfo = $this->tokenizer->peekPrvsValidToken();
if (($position == 'sl') && ($previousTokenInfo->lineOffset != 0)) {
$msg = sprintf(PHPCHECKSTYLE_CS_STMT_ALIGNED_WITH_CURLY, $csText);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('controlStructElse'));
}
if (($position == 'nl') && ($previousTokenInfo->lineOffset == 0)) {
$msg = sprintf(PHPCHECKSTYLE_CS_STMT_ON_NEW_LINE, $csText);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('controlStructElse'));
}
}
}
/**
* Process the start of a control structure (do/while/...).
*/
private function _processControlStructureStart() {
// check for curly braces
if ($this->_config->getTest('controlStructOpenCurly')) {
$pos = $this->_config->getTestProperty('controlStructOpenCurly', 'position');
$previousTokenInfo = $this->tokenizer->peekPrvsValidToken();
if ($pos == "nl") {
// We expect the next token after the curly to be on a new line
$isPosOk = ($previousTokenInfo->lineOffset < 0);
} else {
// We expect the next token after the curly to be on the same line
$isPosOk = ($previousTokenInfo->lineOffset == 0);
}
if (!$isPosOk) {
$tmp = ($pos == "sl") ? "the previous line." : "a new line.";
$msg = sprintf(PHPCHECKSTYLE_LEFT_CURLY_POS, $tmp);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('controlStructOpenCurly'));
}
}
// WARN: used for a very simple (and wrong!) do/while processing
$this->_justAfterControlStmt = false;
}
/**
* Process the start of a function declaration.
*/
private function _processFunctionStart() {
$this->_inFunction = true;
$this->_cyclomaticComplexity = 1;
$this->_functionLevel = $this->_levelOfNesting;
$this->_justAfterFuncStmt = false;
$this->_functionStartLine = $this->lineNumber;
// Check the position of the open curly after the function declaration
if ($this->_config->getTest('funcDefinitionOpenCurly')) {
$pos = $this->_config->getTestProperty('funcDefinitionOpenCurly', 'position');
$previousTokenInfo = $this->tokenizer->peekPrvsValidToken();
if ($pos == "nl") {
// The previous token should be on the previous line
$isPosOk = ($previousTokenInfo->lineOffset < 0);
} else {
// The previous token should be on the same line
$isPosOk = ($previousTokenInfo->lineOffset == 0);
}
if (!$isPosOk) {
$tmp = ($pos == "sl") ? "the previous line." : "a new line.";
$msg = sprintf(PHPCHECKSTYLE_LEFT_CURLY_POS, $tmp);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('funcDefinitionOpenCurly'));
}
}
}
/**
* Process the end of a function declaration.
*/
private function _processFunctionStop() {
$this->_inFunction = false;
// Check cyclomaticComplexity
if ($this->_config->getTest('cyclomaticComplexity')) {
$warningLevel = $this->_config->getTestProperty('cyclomaticComplexity', 'warningLevel');
$errorLevel = $this->_config->getTestProperty('cyclomaticComplexity', 'errorLevel');
if ($this->_cyclomaticComplexity > $warningLevel) {
$msg = sprintf(PHPCHECKSTYLE_CYCLOMATIC_COMPLEXITY, $this->_cyclomaticComplexity);
$this->_reporter->writeError($this->_functionStartLine, $msg, 'WARNING');
} else if ($this->_cyclomaticComplexity > $errorLevel) {
$msg = sprintf(PHPCHECKSTYLE_CYCLOMATIC_COMPLEXITY, $this->_cyclomaticComplexity);
$this->_reporter->writeError($this->_functionStartLine, $msg, 'ERROR');
}
}
//
// Check that the declared parameters in the docblock match the content of the function
//
// If the function is not private and we check the doc
$isPrivateExcluded = $this->_config->getTestProperty('docBlocks', 'excludePrivateMembers');
if (!($isPrivateExcluded && $this->_isFunctionPrivate)) {
// Check the docblock @return
if ($this->_config->getTest('docBlocks') && $this->_config->getTestProperty('docBlocks', 'testReturn') != 'false') {
if ($this->_functionReturns && ($this->_docblocNbReturns == 0)) {
$this->_reporter->writeError($this->_functionStartLine, PHPCHECKSTYLE_DOCBLOCK_RETURN, $this->_config->getTestLevel('docBlocks'));
}
}
// Check the docblock @param
if ($this->_config->getTest('docBlocks') && $this->_config->getTestProperty('docBlocks', 'testParam') != 'false') {
if ($this->_nbFunctionParameters != $this->_docblocNbParams) {
$this->_reporter->writeError($this->_functionStartLine, PHPCHECKSTYLE_DOCBLOCK_PARAM, $this->_config->getTestLevel('docBlocks'));
}
}
// Check the docblock @throw
if ($this->_config->getTest('docBlocks') && $this->_config->getTestProperty('docBlocks', 'testThrow') != 'false') {
if ($this->_functionThrows && ($this->_docblocNbThrows == 0)) {
$this->_reporter->writeError($this->_functionStartLine, PHPCHECKSTYLE_DOCBLOCK_THROW, $this->_config->getTestLevel('docBlocks'));
}
}
}
$this->_docblocNbParams = 0;
$this->_docblocNbReturns = 0;
$this->_docblocNbThrows = 0;
// Check the lenght of the function
if ($this->_config->getTest('functionLength')) {
$maxLength = $this->_config->getTestProperty('functionLength', 'maxLength');
$functionLength = $this->lineNumber - $this->_functionStartLine;
if ($functionLength > $maxLength) {
$msg = sprintf(PHPCHECKSTYLE_FUNCTION_LENGTH_THROW, $functionLength);
$this->_reporter->writeError($this->_functionStartLine, $msg, $this->_config->getTestLevel('functionLength'));
}
}
// Check unused function parameters
$this->_checkUnusedFunctionParameters();
}
/**
* Process a function declaration statement (the parameters).
*/
private function _processFunctionStatement() {
// Increment the number of functions
$this->_ncssTotalFunctions++;
$this->_ncssFileFunctions++;
// Reset the default values
$this->funcArgString = "";
$this->_nbFunctionParameters = 0;
$this->_functionParameters = array();
$this->_inFunctionStatement = true;
$this->_functionReturns = false;
$this->_functionThrows = false;
$this->_inControlStatement = false;
$this->_currentStatement = false;
$this->_inClassStatement = false;
// Check if the function is private or not
$this->_isFunctionPrivate = false;
if ($this->tokenizer->checkPreviousValidToken(T_PRIVATE)) {
$this->_isFunctionPrivate = true; // We are currently in a private function
}
// Skip until T_STRING representing the function name
while (!$this->tokenizer->checkProvidedToken($this->token, T_STRING)) {
$this->_moveToken();
}
// Extract the function name
$functionName = $this->token[1];
// If the function is private we add it to the list of function to use (and store the line number)
if ($this->_isFunctionPrivate) {
$this->_privateFunctions[$functionName] = $functionName;
$this->_privateFunctionsStartLines[$functionName] = $this->lineNumber;
}
// Function is a constructor
if ($functionName == "__construct" && $this->_config->getTest('constructorNaming') && $this->_config->getTestProperty('constructorNaming', 'naming') == 'old') {
$msg = sprintf(PHPCHECKSTYLE_CONSTRUCTOR_NAMING, 'old style : '.$this->_currentClassname);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('constructorNaming'));
}
// Special functions are not checked
if (!in_array($functionName, $this->_specialFunctions)) {
// Constructor
if ($functionName == $this->_currentClassname) {
// Function is a constructor
if ($this->_config->getTest('constructorNaming') && $this->_config->getTestProperty('constructorNaming', 'naming') == 'new') {
$msg = sprintf(PHPCHECKSTYLE_CONSTRUCTOR_NAMING, 'new style : __construct()');
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('constructorNaming'));
}
} else {
if ($this->_isFunctionPrivate) {
$this->_checkPrivateFunctionNaming($functionName);
} else {
$this->_checkFunctionNaming($functionName);
}
}
}
// List the arguments of the currently analyzed function.
// Check the order of the parameters of the function.
// The parameters having a default value should be in last position.
$foundDefaultValues = false;
$functionTokenPosition = $this->tokenizer->getCurrentPosition();
while (true) {
$functionToken = $this->tokenizer->peekTokenAt($functionTokenPosition);
if ($this->tokenizer->checkProvidedText($functionToken, ')')) {
// We found the closing brace
break;
}
// If we find a "=" we consided that the parameter has a default value
$defaultSpecified = $this->tokenizer->checkProvidedText($functionToken, "=");
// We have found one default value for at least one parameter
if ($defaultSpecified) {
$foundDefaultValues = true;
}
// Current token is a parameter
if ($this->tokenizer->checkProvidedToken($functionToken, T_VARIABLE)) {
$this->_nbFunctionParameters++;
$parameterName = $this->tokenizer->extractTokenText($functionToken);
$this->_functionParameters[$parameterName] = "unused"; // We flag the parameter as unused
// Check is this parameter as a default value
$nextTokenInfo = $this->tokenizer->peekNextValidToken($functionTokenPosition + 1);
$hasDefaultValue = $this->tokenizer->checkProvidedText($nextTokenInfo->token, "=");
// Check if the parameter has a default value
if ($this->_config->getTest('defaultValuesOrder')) {
if ($foundDefaultValues && !$hasDefaultValue) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_FUNC_DEFAULTVALUE_ORDER, $this->_config->getTestLevel('defaultValuesOrder'));
break;
}
}
}
$functionTokenPosition++;
}
// Test for the max number of parameters
if ($this->_config->getTest('functionMaxParameters')) {
if ($this->_nbFunctionParameters > $this->_config->getTestProperty('functionMaxParameters', 'maxParameters')) {
$msg = sprintf(PHPCHECKSTYLE_MAX_PARAMETERS, $this->_nbFunctionParameters);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('functionMaxParameters'));
}
}
}
/**
* Process the start of a switch block.
*/
private function _processSwitchStart() {
$this->_inSwitch = true;
$this->_switchLevel = $this->_levelOfNesting;
$this->_switchHasDefault = false;
$this->_caseHasBreak = true;
$this->_switchStartLine = $this->lineNumber;
}
/**
* Process the end of a switch block.
*/
private function _processSwitchStop() {
$this->_inSwitch = false;
if ($this->_config->getTest('switchNeedDefault') && !$this->_switchHasDefault) {
$this->_reporter->writeError($this->_switchStartLine, PHPCHECKSTYLE_SWITCH_DEFAULT, $this->_config->getTestLevel('switchNeedDefault'));
}
}
/**
* Process a case statement.
*/
private function _processSwitchCase() {
// Test if the previous case had a break
if ($this->_config->getTest('switchCaseNeedBreak') && !$this->_caseHasBreak) {
$this->_reporter->writeError($this->_caseStartLine, PHPCHECKSTYLE_SWITCH_CASE_NEED_BREAK, $this->_config->getTestLevel('switchCaseNeedBreak'));
}
// If the case arrives after the default
if ($this->_config->getTest('switchDefaultOrder') && $this->_switchHasDefault) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_SWITCH_DEFAULT_ORDER, $this->_config->getTestLevel('switchDefaultOrder'));
}
// For this break
$this->_caseHasBreak = false;
$this->_caseStartLine = $this->lineNumber;
}
/**
* Process a default statement.
*/
private function _processSwitchDefault() {
$this->_switchHasDefault = true;
// Test if the previous case had a break
if ($this->_config->getTest('switchCaseNeedBreak') && !$this->_caseHasBreak) {
$this->_reporter->writeError($this->_caseStartLine, PHPCHECKSTYLE_SWITCH_CASE_NEED_BREAK, $this->_config->getTestLevel('switchCaseNeedBreak'));
}
}
/**
* Process a break statement.
*/
private function _processSwitchBreak() {
$this->_caseHasBreak = true;
}
/**
* Process a class declaration statement.
*/
private function _processClassStatement() {
$this->_ncssTotalClasses++;
$this->_ncssFileClasses++;
// Test if there is more than one class per file
if ($this->_config->getTest('oneClassPerFile') && $this->_ncssFileClasses > 1) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_ONE_CLASS_PER_FILE, $this->_config->getTestLevel('oneClassPerFile'));
}
// Reset the default values
$this->_inFunction = false;
$this->_nbFunctionParameters = 0;
$this->_functionParameters = array();
$this->_inFunctionStatement = false;
$this->_functionReturns = false;
$this->_functionThrows = false;
$this->_inControlStatement = false;
$this->_currentStatement = false;
$this->_inClassStatement = true;
// skip until T_STRING representing the class name
while (!$this->tokenizer->checkProvidedToken($this->token, T_STRING)) {
$this->_moveToken();
}
// class name has to start with uppercase
$classname = $this->token[1];
$this->_currentClassname = $classname;
// Check class naming
$this->_checkClassNaming($classname);
$this->_checkWhiteSpaceAfter($classname);
}
/**
* Check for empty block.
*
* This function is launched when the current token is {
*/
private function _checkEmptyBlock() {
// If the next valid token is } then the statement is empty.
if ($this->_config->getTest('checkEmptyBlock') && $this->_currentStatement) {
if ($this->tokenizer->checkNextValidTextToken("}")) {
$msg = sprintf(PHPCHECKSTYLE_EMPTY_BLOCK, $this->_currentStatement);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('checkEmptyBlock'));
}
}
}
/**
* Check for inner assignments.
*
* This function is launched when the current token is = (assignment).
*/
private function _checkInnerAssignment() {
// If the test if active and we are inside a control statement
if ($this->_config->getTest('checkInnerAssignment') && $this->_inControlStatement) {
// If the control statement is not listed as an exception
$exceptions = $this->_config->getTestExceptions('checkInnerAssignment');
if (empty($exceptions) || !in_array($this->_currentStatement, $exceptions)) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_INSIDE_ASSIGNMENT, $this->_config->getTestLevel('checkInnerAssignment'));
}
}
}
/**
* Check for empty statement.
*
* This function is launched when the current token is ;
*/
private function _checkEmptyStatement() {
// If the next valid token is ; then the statement is empty.
if ($this->_config->getTest('checkEmptyStatement') && $this->_currentStatement) {
if ($this->tokenizer->checkNextValidTextToken(";")) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_EMPTY_STATEMENT, $this->_config->getTestLevel('checkEmptyStatement'));
}
}
}
/**
* Check for unused functions.
*
* This function is launched at the end of a file.
*/
private function _checkUnusedPrivateFunctions() {
if ($this->_config->getTest('checkUnusedPrivateFunctions')) {
// We make a diff between the list of declared private functions and the list of called functions.
// This is a very simple and approximative test, we don't test that the called function is from the good class.
// The usedFunctions array contains a lot of false positives
$uncalledFunctions = array_diff($this->_privateFunctions, $this->_usedFunctions);
foreach ($uncalledFunctions as $uncalledFunction) {
$msg = sprintf(PHPCHECKSTYLE_UNUSED_PRIVATE_FUNCTION, $uncalledFunction);
$this->_reporter->writeError($this->_privateFunctionsStartLines[$uncalledFunction], $msg, $this->_config->getTestLevel('checkUnusedPrivateFunctions'));
}
}
}
/**
* Check for unused variables in the file.
*
* This function is launched at the end of a file.
*/
private function _checkUnusedVariables() {
if ($this->_config->getTest('checkUnusedVariables')) {
foreach ($this->_variables as $variableName => $value) {
if (($value != "used") && !($this->_isClass || $this->_isView)) {
$msg = sprintf(PHPCHECKSTYLE_UNUSED_VARIABLE, $variableName);
$this->_reporter->writeError($value, $msg, $this->_config->getTestLevel('checkUnusedVariables'));
}
}
}
}
/**
* Check for unused function parameters.
*
* This function is launched at the end of a function
*/
private function _checkUnusedFunctionParameters() {
if ($this->_config->getTest('checkUnusedFunctionParameters')) {
foreach ($this->_functionParameters as $variableName => $value) {
if ($value != "used") {
$msg = sprintf(PHPCHECKSTYLE_UNUSED_FUNCTION_PARAMETER, $variableName);
$this->_reporter->writeError($this->_functionStartLine, $msg, $this->_config->getTestLevel('checkUnusedFunctionParameters'));
}
}
}
}
/**
* Check the variable use.
*
* This function is launched when the current token is T_VARIABLE
*/
private function _processVariable($text) {
// Check the variable naming
if (!in_array($text, $this->_systemVariables)) {
$this->_checkVariableNaming($text);
}
// Check if the variable is a function parameter
if (!empty($this->_functionParameters[$text]) && $this->_inFunction) {
$this->_functionParameters[$text] = "used";
} else if (!$this->_inFunctionStatement) {
// Global variable
$pos = $this->tokenizer->getCurrentPosition();
$nextTokenInfo = $this->tokenizer->peekNextValidToken($pos);
// if the next token is an equal, we suppose that this is an affectation
$isAffectation = $this->tokenizer->checkProvidedText($nextTokenInfo->token, "=");
// Check if the variable has already been met
if (empty($this->_variables[$text]) && !in_array($text, $this->_systemVariables)) {
// The variable is met for the first time
$this->_variables[$text] = $this->lineNumber; // We store the first declaration of the variable
} else if ($isAffectation) {
// The variable is reaffected another value, this doesn't count as a valid use.
} else {
// Manage the case of $this->attribute
if ($text == '$this') {
if ($this->tokenizer->checkProvidedToken($nextTokenInfo->token, T_OBJECT_OPERATOR)) {
$nextTokenInfo2 = $this->tokenizer->peekNextValidToken($nextTokenInfo->position);
// This does not look like a function call, it should be a class attribute.
// We eliminate the $this-> part
$text = '$'.$this->tokenizer->extractTokenText($nextTokenInfo2->token);
}
}
// The variable is met again, we suppose we have used it for something
$this->_variables[$text] = "used";
}
}
}
/**
* Check the return token.
*
* This function is launched when the current token is T_RETURN
*/
private function _processReturn() {
// Remember that the current function does return something (for PHPDoc)
$this->_functionReturns = true;
// Search for unused code
if ($this->_config->getTest('checkUnusedCode')) {
// Look ahead
$stop = false;
$nbInstructions = 0;
$position = $this->tokenizer->getCurrentPosition();
while (!$stop) {
$token = $this->tokenizer->peekTokenAt($position);
if (is_string($token)) {
if ($token == ";") {
$nbInstructions++;
if ($nbInstructions > 1) {
// more than 1 instruction after the return and before the closing of the statement
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_UNUSED_CODE, $this->_config->getTestLevel('checkUnusedCode'));
$stop = true;
}
} else if ($token == "}") {
$stop = true;
}
}
$position++;
}
}
}
/**
* Check for heredoc syntax.
*
* This function is launched when the current token is T_START_HEREDOC
*/
private function _checkHeredoc() {
if ($this->_config->getTest('checkHeredoc')) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_HEREDOC, $this->_config->getTestLevel('checkHeredoc'));
}
}
/**
* Check for the presence of a white space before and after the text.
*
* @param String $text The text of the token to test
*/
private function _checkSurroundingWhiteSpace($text) {
$this->_checkWhiteSpaceBefore($text);
$this->_checkWhiteSpaceAfter($text);
}
/**
* Check for the presence of a white space before the text.
*
* @param String $text The text of the token to test
*/
private function _checkWhiteSpaceBefore($text) {
if ($this->_config->getTest('checkWhiteSpaceBefore')) {
$exceptions = $this->_config->getTestExceptions('checkWhiteSpaceBefore');
if (empty($exceptions) || !in_array($text, $exceptions)) {
if (!$this->tokenizer->checkProvidedToken($this->prvsToken, T_WHITESPACE)) {
$msg = sprintf(PHPCHECKSTYLE_SPACE_BEFORE_TOKEN, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('checkWhiteSpaceBefore'));
}
}
}
}
/**
* Check for the abscence of a white space before the text.
*
* @param String $text The text of the token to test
*/
private function _checkNoWhiteSpaceBefore($text) {
if ($this->_config->getTest('noSpaceBeforeToken')) {
$exceptions = $this->_config->getTestExceptions('noSpaceBeforeToken');
if (empty($exceptions) || !in_array($text, $exceptions)) {
if ($this->tokenizer->checkProvidedToken($this->prvsToken, T_WHITESPACE)) {
$msg = sprintf(PHPCHECKSTYLE_NO_SPACE_BEFORE_TOKEN, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('noSpaceBeforeToken'));
}
}
}
}
/**
* Check for the presence of a white space after the text.
*
* @param String $text The text of the token to test
*/
private function _checkWhiteSpaceAfter($text) {
if ($this->_config->getTest('checkWhiteSpaceAfter')) {
$exceptions = $this->_config->getTestExceptions('checkWhiteSpaceAfter');
if (empty($exceptions) || !in_array($text, $exceptions)) {
if (!$this->tokenizer->checkNextToken(T_WHITESPACE)) {
// In case of new line or a php closing tag it's OK
if (!($this->tokenizer->checkNextToken(T_NEW_LINE) || $this->tokenizer->checkNextToken(T_CLOSE_TAG))) {
$msg = sprintf(PHPCHECKSTYLE_SPACE_AFTER_TOKEN, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('checkWhiteSpaceAfter'));
}
}
}
}
}
/**
* Check for the abscence of a white space after the text.
*
* @param String $text The text of the token to test
*/
private function _checkNoWhiteSpaceAfter($text) {
if ($this->_config->getTest('noSpaceAfterToken')) {
$exceptions = $this->_config->getTestExceptions('noSpaceAfterToken');
if (empty($exceptions) || !in_array($text, $exceptions)) {
if ($this->tokenizer->checkNextToken(T_WHITESPACE)) {
$msg = sprintf(PHPCHECKSTYLE_NO_SPACE_AFTER_TOKEN, $text);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('noSpaceAfterToken'));
}
}
}
}
/**
* Avoid using unary operators (++ or --) inside a control statement.
*/
private function _checkUnaryOperator() {
if ($this->_config->getTest('checkUnaryOperator')) {
// If the control statement is not listed as an exception
$exceptions = $this->_config->getTestExceptions('checkUnaryOperator');
if (empty($exceptions) || !in_array($this->_currentStatement, $exceptions) || $this->_inArrayStatement) {
// And if we are currently in a control statement or an array statement
if ($this->_inControlStatement || $this->_inArrayStatement) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_UNARY_OPERATOR, $this->_config->getTestLevel('checkUnaryOperator'));
}
}
}
}
/**
* Move to the next token.
*/
private function _moveToken() {
$this->prvsToken = $this->token;
$this->token = $this->tokenizer->getNextToken();
$isHtml = $this->tokenizer->checkProvidedToken($this->token, T_INLINE_HTML);
// Test the lenght of the line
if (!$isHtml && $this->_config->getTest('lineLength')) {
$this->_checkLargeLine();
}
}
/**
* Check if the current line exceeds the maxLineLength allowed.
*/
private function _checkLargeLine() {
if ($this->_config->getTest('lineLength')) {
// Comments are ignored
if (!($this->tokenizer->checkProvidedToken($this->token, T_COMMENT) || $this->tokenizer->checkProvidedToken($this->token, T_ML_COMMENT) || $this->tokenizer->checkProvidedToken($this->token, T_DOC_COMMENT))) {
$text = $this->tokenizer->extractTokenText($this->token);
$text = trim($text);
$maxlen = $this->_config->getTestProperty('lineLength', 'maxLineLength');
$msg = sprintf(PHPCHECKSTYLE_LONG_LINE, $maxlen);
if (strlen($text) > $maxlen) {
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('lineLength'));
}
}
}
}
/**
* Only checks for presence of tab in the whitespace character string
*
* @param String $ws
*/
private function _checkIndentation($ws) {
if ($this->_config->getTest('noTabs')) {
$tabfound = preg_match("/\t/", $ws);
if ($tabfound) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_TAB_IN_LINE, $this->_config->getTestLevel('noTabs'));
}
}
}
/**
* Checks if the block of code need braces.
*
* This function is called we the current token is ) and we are in a control statement.
*/
private function _checkNeedBraces() {
if ($this->_config->getTest('needBraces')) {
$stmt = strtolower($this->_currentStatement);
if ($stmt == "if" || $stmt == "else" || $stmt == "elseif" || $stmt == "do" || $stmt == "while" || $stmt == "for" || $stmt == "foreach") {
if (!$this->tokenizer->checkNextValidTextToken("{")) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_NEED_BRACES, $this->_config->getTestLevel('needBraces'));
}
}
}
}
/**
* Process a comment.
*
* @param $tol the token
* @param $text the text of the comment
*/
private function _processComment($tok, $text) {
// Count the lines of comment
if ($tok == T_COMMENT) {
$this->_ncssTotalSingleComment++;
$this->_ncssFileSingleComment++;
} else if ($tok == T_ML_COMMENT) {
$this->_ncssTotalMultiComment++;
$this->_ncssFileMultiComment++;
} else if ($tok == T_DOC_COMMENT) {
$this->_ncssTotalPhpdoc++;
$this->_ncssFilePhpdoc++;
}
// Manage new lines inside commments
$subToken = strtok($text, PHP_EOL);
while ($subToken !== false) {
if ($tok == T_DOC_COMMENT) {
$this->_ncssTotalLinesPhpdoc++;
$this->_ncssFileLinesPhpdoc++;
}
// Count the @params, @returns and @throw
if (stripos($subToken, '@param') !== false) {
$this->_docblocNbParams++;
}
if (stripos($subToken, '@return') !== false) {
$this->_docblocNbReturns++;
}
if (stripos($subToken, '@throw') !== false) {
$this->_docblocNbThrows++;
}
// Increment the lines number (one comment is only one token)
$this->lineNumber++;
$subToken = strtok(PHP_EOL);
}
$this->lineNumber--; // One end of line is already counted
// Check if the comment starts with '#'
if ($this->_config->getTest("noShellComments")) {
$s = strpos($text, '#');
if ($s === 0) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_NO_SHELL_COMMENTS, $this->_config->getTestLevel('noShellComments'));
}
}
// Check if the comment contains a TODO
if ($this->_config->getTest("showTODOs")) {
$s = strpos($text, 'TODO');
if ($s != FALSE) {
$msg = sprintf(PHPCHECKSTYLE_TODO, substr($text, strpos($text, 'TODO') + 4));
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('showTODOs'));
}
}
}
/**
* Check for the existence of a docblock for the current token
* o go back and find the previous token that is not a whitespace
* o if it is a access specifier (private, public etc), then
* see if private members are excluded from comment check
* (input argument specified this). if we find an access
* specifier move on to find the next best token
* o if it is ABSTRACT or STATIC specifier move on to find the next best token
* o if the found token is a T_DOC_COMMENT, then we have a docblock
*
* This, of course, assumes that the function or the class has to be
* immediately preceded by docblock. Even regular comments are not
* allowed, which I think is okay.
*
* @param $isPrivate specify whether private members are excluded from test
* @return true is docblock is found
*/
private function _checkDocExists() {
// current token = the token after T_CLASS or T_FUNCTION
//
// token positions:
// . curToken - 1 = T_CLASS/T_FUNCTION
// . curToken - 2 = whitespace before T_CLASS/T_FUNCTION
// . curToken - 3 = T_ABSTRACT/T_PUBLIC/T_PROTECTED/T_PRIVATE/T_STATIC
// or T_DOC_COMMENT, if it is present
//
if ($this->_config->getTest('docBlocks')) {
$isPrivate = $this->_config->getTestProperty('docBlocks', 'excludePrivateMembers');
// Locate the function or class token
$functionTokenPosition = $this->tokenizer->getCurrentPosition();
while (true) {
$functionToken = $this->tokenizer->peekTokenAt($functionTokenPosition);
if ($this->tokenizer->checkProvidedToken($functionToken, T_FUNCTION) || $this->tokenizer->checkProvidedToken($functionToken, T_CLASS)) {
break;
}
$functionTokenPosition--;
}
$found = false;
$docTokenPosition = $functionTokenPosition - 1;
// Go backward and look for a T_DOC_COMMENT
while (true) {
$docToken = $this->tokenizer->peekTokenAt($docTokenPosition);
if (is_array($docToken)) {
if ($this->tokenizer->checkProvidedToken($docToken, T_STATIC) || $this->tokenizer->checkProvidedToken($docToken, T_ABSTRACT) || $this->tokenizer->checkProvidedToken($docToken, T_PROTECTED) || $this->tokenizer->checkProvidedToken($docToken, T_PUBLIC) || $this->tokenizer->checkProvidedToken($docToken, T_WHITESPACE) || $this->tokenizer->checkProvidedToken($docToken, T_COMMENT) || $this->tokenizer->checkProvidedToken($docToken, T_ML_COMMENT) || $this->tokenizer->checkProvidedToken($docToken, T_NEW_LINE)) {
// All these tokens are ignored
} else if ($this->tokenizer->checkProvidedToken($docToken, T_PRIVATE)) {
// We don't care if we find anything, private functions are not tested
if ($isPrivate) {
$found = true;
}
break;
} else if ($this->tokenizer->checkProvidedToken($docToken, T_DOC_COMMENT)) {
// We have found a doc comment
$found = true;
break;
} else {
// Any other token found, we stop
$found = false;
break;
}
} else {
// The previous token is a string
break;
}
$docTokenPosition--;
if ($docTokenPosition == 0) {
break;
}
}
if (!$found) {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_MISSING_DOCBLOCK, $this->_config->getTestLevel('docBlocks'));
}
}
}
/**
* Display the current branching stack?
*/
private function _dumpStack() {
foreach ($this->_branchingStack as $item) {
echo $item."->";
}
echo PHP_EOL;
}
/**
* Check for silenced call to functons.
*
* @param String $text The text of the token to test
*/
private function _checkSilenced($text) {
if ($this->_config->getTest('checkSilencedError')) {
$exceptions = $this->_config->getTestExceptions('checkSilencedError');
if (empty($exceptions) || !in_array($text, $exceptions)) {
if ($this->prvsToken == "@") {
$this->_reporter->writeError($this->lineNumber, PHPCHECKSTYLE_SILENCED_ERROR, $this->_config->getTestLevel('checkSilencedError'));
}
}
}
}
/**
* Check for deprecated functions.
*
* @param String $text The text of the token to test
*/
private function _checkDeprecation($text) {
if ($this->_config->getTest('checkDeprecation')) {
if (array_key_exists($text, $this->_deprecatedFunctions)) {
$msg = sprintf(PHPCHECKSTYLE_DEPRECATED_FUNCTION, $this->_deprecatedFunctions[$text]['old'], $this->_deprecatedFunctions[$text]['version'], $this->_deprecatedFunctions[$text]['new']);
$this->_reporter->writeError($this->lineNumber, $msg, $this->_config->getTestLevel('checkDeprecation'));
}
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.