Skip to content

Conversation

@sanmai
Copy link
Member

@sanmai sanmai commented Jun 27, 2025

  • Install PHPStan at level 0
  • Fix few issues that PHPStan found:
    • Add missing return statement in Parser::_convertFunction()
    • Fix regex patterns by properly escaping $ characters
    • Replace unset() with null assignment for PHP 8.4 compatibility
    • Fix duplicate array keys in ptg definitions
  • Add unit tests to verify the fixes
  • Add dedicated static analysis workflow using PHP 8.2

Reference: https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/9310c3bb-d73f-4db0-8342-28e1e0fcb68f

sanmai added 4 commits June 27, 2025 17:13
- Install PHPStan at level 0
- Fix few issues that PHPStan found:
  - Add missing return statement in Parser::_convertFunction()
  - Fix regex patterns by properly escaping $ characters
  - Replace unset() with null assignment for PHP 8.4 compatibility
  - Fix duplicate array keys in ptg definitions
- Add unit tests to verify the fixes
- Add dedicated static analysis workflow using PHP 8.2
PHPStan correctly identified that the Parser's $ptg array had duplicate keys,
which is invalid in PHP. The Excel PTG specification legitimately has some
opcodes with the same name at different positions:

- ptgMemNoMem appears at 0x28, 0x48, 0x68 (per Microsoft spec)
- ptgAreaErr3d appears at 0x3D, 0x5D, 0x7D (per Microsoft spec)
- ptgMemNoMemN had invalid duplicates at 0x2F, 0x4F, 0x6F (not in spec)

In PHP arrays, duplicate keys result in the last value overwriting earlier ones.
To maintain backward compatibility, we:

1. Keep the LAST occurrence of each duplicate (BC preserved)
2. Comment out earlier duplicates to satisfy PHPStan
3. Remove incorrectly suffixed variants (ptgMemNoMemV/A) that don't exist in spec
4. Add comprehensive tests to ensure BC is maintained

This preserves existing behavior while fixing the static analysis errors.
@sanmai sanmai requested a review from Copilot June 28, 2025 10:26

This comment was marked as outdated.

@sanmai sanmai changed the title Add PHPStan static analysis, minimal fixes Add PHPStan static analysis at level 0, minimal fixes Jun 28, 2025
@sanmai sanmai requested a review from Copilot June 28, 2025 10:31
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces PHPStan static analysis at level 0, makes minimal code fixes discovered by the analysis, and updates the CI to run the new workflow.

  • Add PHPStan configuration and GitHub Actions workflow for static analysis
  • Fix missing return in Parser::_convertFunction, adjust unset to null in Worksheet::getData, and update duplicate PTG definitions
  • Enhance tests to cover the new behaviors and update composer.json dev dependencies

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/Test/Spreadsheet/Excel/Writer/WorksheetTest.php Add assertion that _data is now null after getData()
test/Test/Spreadsheet/Excel/Writer/ParserTest.php Extend _convertFunction tests with invalid-args exception path
phpstan.neon Introduce PHPStan at level 0 and ignore one known error
composer.json Include phpstan/phpstan and php-parallel-lint; sort packages
Spreadsheet/Excel/Writer/Worksheet.php Replace unset($this->_data) with assignment to null
Spreadsheet/Excel/Writer/Parser.php Comment out duplicate PTG keys and add exception for bad args
.github/workflows/static-analysis.yaml New workflow to run PHPStan analysis on PHP 8.2
.github/workflows/ci.yaml Remove phpstan/phpstan from default CI dev dependencies
Comments suppressed due to low confidence (1)

Spreadsheet/Excel/Writer/Parser.php:675

  • Add an @throws \InvalidArgumentException tag to the _convertFunction docblock to document the new exception path.
        throw new \InvalidArgumentException("Invalid argument count $args for function $token");

$this->assertTrue(is_string($result));

// Array structure: [function_number, arg_count, unknown, volatile_flag]
$parser->_functions['INVALID'] = array(999, -2, 0, 0); // -2 is not valid
Copy link

Copilot AI Jun 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly assigning to _functions may fail if the property is protected. Use ReflectionProperty to make _functions accessible or provide a setter method in Parser for testing.

Copilot uses AI. Check for mistakes.
@sanmai sanmai merged commit da9cec7 into pear:master Jun 28, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant