Skip to content

Commit

Permalink
Introduce PHPStan
Browse files Browse the repository at this point in the history
To improve the feedback loop on code quality with a process
that can be run locally by the developers, instead of only
on Scrutinizer.
  • Loading branch information
PowerKiKi committed Apr 3, 2021
1 parent a2bb825 commit a189d93
Show file tree
Hide file tree
Showing 34 changed files with 375 additions and 217 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@ jobs:
- name: Code style with PHP_CodeSniffer
run: ./vendor/bin/phpcs -q --report=checkstyle | cs2pr

phpstan:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
coverage: none
tools: cs2pr

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Static analysis with PHPStan
run: ./vendor/bin/phpstan analyse

coverage:
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"check": [
"php-cs-fixer fix --ansi --dry-run --diff",
"phpcs",
"phpunit --color=always"
"phpunit --color=always",
"phpstan analyse --ansi"
],
"fix": [
"php-cs-fixer fix --ansi"
Expand Down Expand Up @@ -79,6 +80,7 @@
"jpgraph/jpgraph": "^4.0",
"mpdf/mpdf": "^8.0",
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^0.12.82",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5",
"tecnickcom/tcpdf": "^6.3"
Expand Down
62 changes: 61 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parameters:
level: 1
paths:
- src/
- tests/
ignoreErrors:
- '~^Class GdImage not found\.$~'

# Ignore all JpGraph issues
- '~^Constant (MARK_CIRCLE|MARK_CROSS|MARK_DIAMOND|MARK_DTRIANGLE|MARK_FILLEDCIRCLE|MARK_SQUARE|MARK_STAR|MARK_UTRIANGLE|MARK_X|SIDE_RIGHT) not found\.$~'
- '~^Instantiated class (AccBarPlot|AccLinePlot|BarPlot|ContourPlot|Graph|GroupBarPlot|GroupBarPlot|LinePlot|LinePlot|PieGraph|PiePlot|PiePlot3D|PiePlotC|RadarGraph|RadarPlot|ScatterPlot|Spline|StockPlot) not found\.$~'
27 changes: 19 additions & 8 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,7 @@ public static function wrapResult($value)
// Return Excel errors "as is"
return $value;
}

// Return strings wrapped in quotes
return self::FORMULA_STRING_QUOTE . $value . self::FORMULA_STRING_QUOTE;
} elseif ((is_float($value)) && ((is_nan($value)) || (is_infinite($value)))) {
Expand Down Expand Up @@ -3794,13 +3795,13 @@ private function internalParseFormula($formula, ?Cell $pCell = null)
$pCellParent = ($pCell !== null) ? $pCell->getWorksheet() : null;

$regexpMatchString = '/^(' . self::CALCULATION_REGEXP_FUNCTION .
'|' . self::CALCULATION_REGEXP_CELLREF .
'|' . self::CALCULATION_REGEXP_NUMBER .
'|' . self::CALCULATION_REGEXP_STRING .
'|' . self::CALCULATION_REGEXP_OPENBRACE .
'|' . self::CALCULATION_REGEXP_DEFINEDNAME .
'|' . self::CALCULATION_REGEXP_ERROR .
')/sui';
'|' . self::CALCULATION_REGEXP_CELLREF .
'|' . self::CALCULATION_REGEXP_NUMBER .
'|' . self::CALCULATION_REGEXP_STRING .
'|' . self::CALCULATION_REGEXP_OPENBRACE .
'|' . self::CALCULATION_REGEXP_DEFINEDNAME .
'|' . self::CALCULATION_REGEXP_ERROR .
')/sui';

// Start with initialisation
$index = 0;
Expand Down Expand Up @@ -3939,6 +3940,7 @@ private function internalParseFormula($formula, ?Cell $pCell = null)
}
// Check the argument count
$argumentCountError = false;
$expectedArgumentCountString = null;
if (is_numeric($expectedArgumentCount)) {
if ($expectedArgumentCount < 0) {
if ($argumentCount > abs($expectedArgumentCount)) {
Expand Down Expand Up @@ -4203,7 +4205,7 @@ private function internalParseFormula($formula, ?Cell $pCell = null)
((preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '.*/Ui', substr($formula, $index), $match)) &&
($output[count($output) - 1]['type'] == 'Cell Reference') ||
(preg_match('/^' . self::CALCULATION_REGEXP_DEFINEDNAME . '.*/miu', substr($formula, $index), $match)) &&
($output[count($output) - 1]['type'] == 'Defined Name' || $output[count($output) - 1]['type'] == 'Value')
($output[count($output) - 1]['type'] == 'Defined Name' || $output[count($output) - 1]['type'] == 'Value')
)
) {
while (
Expand Down Expand Up @@ -4645,6 +4647,9 @@ private function processTokenStack($tokens, $cellID = null, ?Cell $pCell = null)
$this->debugLog->writeDebugLog('Evaluating Function ', self::localeFunc($functionName), '() with ', (($argCount == 0) ? 'no' : $argCount), ' argument', (($argCount == 1) ? '' : 's'));
}
if ((isset(self::$phpSpreadsheetFunctions[$functionName])) || (isset(self::$controlFunctions[$functionName]))) { // function
$passByReference = false;
$passCellReference = false;
$functionCall = null;
if (isset(self::$phpSpreadsheetFunctions[$functionName])) {
$functionCall = self::$phpSpreadsheetFunctions[$functionName]['functionCall'];
$passByReference = isset(self::$phpSpreadsheetFunctions[$functionName]['passByReference']);
Expand Down Expand Up @@ -4945,6 +4950,9 @@ private function executeBinaryComparisonOperation($cellID, $operand1, $operand2,
}

break;

default:
throw new Exception('Unsupported binary comparison operation');
}

// Log the result details
Expand Down Expand Up @@ -5062,6 +5070,9 @@ private function executeNumericBinaryOperation($operand1, $operand2, $operation,
$result = $operand1 ** $operand2;

break;

default:
throw new Exception('Unsupported numeric binary operation');
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public static function WEEKNUM($dateValue = 1, $method = self::STARTWEEK_SUNDAY)
*
* Returns the ISO 8601 week number of the year for a specified date.
*
* @Deprecated 2.0.0 Use the funcIsoWeeknum method in the DateTimeExcel\Isoweeknum class instead
* @Deprecated 2.0.0 Use the funcIsoWeeknum method in the DateTimeExcel\IsoWeekNum class instead
*
* Excel Function:
* ISOWEEKNUM(dateValue)
Expand All @@ -663,7 +663,7 @@ public static function WEEKNUM($dateValue = 1, $method = self::STARTWEEK_SUNDAY)
*/
public static function ISOWEEKNUM($dateValue = 1)
{
return DateTimeExcel\IsoweekNum::funcIsoWeekNum($dateValue);
return DateTimeExcel\IsoWeekNum::funcIsoWeekNum($dateValue);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Calculation/Financial.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ private static function interestAndPrincipal($rate = 0, $per = 0, $nper = 0, $pv
{
$pmt = self::PMT($rate, $nper, $pv, $fv, $type);
$capital = $pv;
$interest = 0;
$principal = 0;
for ($i = 1; $i <= $per; ++$i) {
$interest = ($type && $i == 1) ? 0 : -$capital * $rate;
$principal = $pmt - $interest;
Expand Down
4 changes: 4 additions & 0 deletions src/PhpSpreadsheet/Chart/Renderer/JpGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ private function renderPlotLine($groupID, $filled = false, $combination = false,
$seriesPlots = [];
if ($grouping == 'percentStacked') {
$sumValues = $this->percentageSumCalculation($groupID, $seriesCount);
} else {
$sumValues = [];
}

// Loop through each data series in turn
Expand Down Expand Up @@ -376,6 +378,8 @@ private function renderPlotBar($groupID, $dimensions = '2d'): void
$seriesPlots = [];
if ($grouping == 'percentStacked') {
$sumValues = $this->percentageSumCalculation($groupID, $seriesCount);
} else {
$sumValues = [];
}

// Loop through each data series in turn
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static function createReaderForFile($filename)
$reader = self::createReader($guessedReader);

// Let's see if we are lucky
if (isset($reader) && $reader->canRead($filename)) {
if ($reader->canRead($filename)) {
return $reader;
}
}
Expand Down
Loading

0 comments on commit a189d93

Please sign in to comment.