From df302a68c8c9ae219b3de5666ac9f0815c0cdb45 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 21 Sep 2023 12:45:01 +0200 Subject: [PATCH] Github Action : PHPCSFixer --- .github/workflows/php.yml | 24 ++++++++++++ .gitignore | 1 + .php-cs-fixer.dist.php | 45 ++++++++++++++++++++++ composer.json | 3 +- src/Math/Element/AbstractElement.php | 6 +-- src/Math/Element/AbstractGroupElement.php | 2 +- src/Math/Element/Fraction.php | 2 +- src/Math/Element/Identifier.php | 2 +- src/Math/Element/Numeric.php | 2 +- src/Math/Element/Operator.php | 2 +- src/Math/Element/Row.php | 2 +- src/Math/Element/Superscript.php | 2 +- src/Math/Math.php | 4 +- src/Math/Reader/MathML.php | 13 ++++--- src/Math/Reader/OfficeMathML.php | 16 ++++---- src/Math/Reader/ReaderInterface.php | 2 +- src/Math/Writer/MathML.php | 15 +++++--- src/Math/Writer/OfficeMathML.php | 15 ++++---- src/Math/Writer/WriterInterface.php | 2 +- tests/Math/Reader/MathMLTest.php | 10 ++--- tests/Math/Reader/OfficeMathMLTest.php | 47 +++++++++++------------ tests/Math/Writer/MathMLTest.php | 17 ++++---- tests/Math/Writer/OfficeMathMLTest.php | 8 ++-- tests/Math/Writer/WriterTestCase.php | 4 +- 24 files changed, 164 insertions(+), 82 deletions(-) create mode 100644 .php-cs-fixer.dist.php diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index aafa3ed..77b3aa5 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -6,6 +6,30 @@ on: pull_request: jobs: + php-cs-fixer: + name: PHP CS Fixer + runs-on: ubuntu-latest + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: xml + + - uses: actions/checkout@v2 + + - name: Validate composer config + run: composer validate --strict + + - name: Composer Install + run: composer global require friendsofphp/php-cs-fixer + + - name: Add environment path + run: export PATH="$PATH:$HOME/.composer/vendor/bin" + + - name: Run PHPCSFixer + run: php-cs-fixer fix --dry-run --diff + phpunit: name: PHPUnit runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 2b91b07..5c8be80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.php_cs.cache .phpunit.cache .phpunit.result.cache composer.lock diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..aaa5016 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,45 @@ +setUsingCache(true) + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + 'array_indentation' => true, + 'cast_spaces' => [ + 'space' => 'single', + ], + 'combine_consecutive_issets' => true, + 'concat_space' => [ + 'spacing' => 'one', + ], + 'error_suppression' => [ + 'mute_deprecation_error' => false, + 'noise_remaining_usages' => false, + 'noise_remaining_usages_exclude' => [], + ], + 'function_to_constant' => false, + 'global_namespace_import' => true, + 'method_chaining_indentation' => true, + 'no_alias_functions' => false, + 'no_superfluous_phpdoc_tags' => false, + 'non_printable_character' => [ + 'use_escape_sequences_in_strings' => true, + ], + 'phpdoc_align' => [ + 'align' => 'left', + ], + 'phpdoc_summary' => false, + 'protected_to_private' => false, + 'self_accessor' => false, + 'yoda_style' => false, + 'single_line_throw' => false, + 'no_alias_language_construct_call' => false, + ]) + ->getFinder() + ->in(__DIR__) + ->exclude('vendor'); + +return $config; \ No newline at end of file diff --git a/composer.json b/composer.json index 4b68d08..7637fb8 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ ], "require": { "php": "^7.1|^8.0", - "ext-xml": "*" + "ext-xml": "*", + "friendsofphp/php-cs-fixer": "^2.1" }, "require-dev": { "phpunit/phpunit": ">=7.0" diff --git a/src/Math/Element/AbstractElement.php b/src/Math/Element/AbstractElement.php index a86335d..be7d4a8 100644 --- a/src/Math/Element/AbstractElement.php +++ b/src/Math/Element/AbstractElement.php @@ -8,16 +8,16 @@ abstract class AbstractElement * @var string */ protected $parent; - + public function setParent($parent): self { $this->parent = $parent; return $this; } - + public function getParent() { return $this->parent; } -} \ No newline at end of file +} diff --git a/src/Math/Element/AbstractGroupElement.php b/src/Math/Element/AbstractGroupElement.php index 395135e..85d97f3 100644 --- a/src/Math/Element/AbstractGroupElement.php +++ b/src/Math/Element/AbstractGroupElement.php @@ -35,4 +35,4 @@ public function getElements(): array { return $this->elements; } -} \ No newline at end of file +} diff --git a/src/Math/Element/Fraction.php b/src/Math/Element/Fraction.php index 8b585ba..511ac99 100644 --- a/src/Math/Element/Fraction.php +++ b/src/Math/Element/Fraction.php @@ -37,4 +37,4 @@ public function setNumerator(AbstractElement $element): self return $this; } -} \ No newline at end of file +} diff --git a/src/Math/Element/Identifier.php b/src/Math/Element/Identifier.php index 95565e5..d2439fa 100644 --- a/src/Math/Element/Identifier.php +++ b/src/Math/Element/Identifier.php @@ -18,4 +18,4 @@ public function getValue(): string { return $this->value; } -} \ No newline at end of file +} diff --git a/src/Math/Element/Numeric.php b/src/Math/Element/Numeric.php index 355f354..e809e61 100644 --- a/src/Math/Element/Numeric.php +++ b/src/Math/Element/Numeric.php @@ -18,4 +18,4 @@ public function getValue(): float { return $this->value; } -} \ No newline at end of file +} diff --git a/src/Math/Element/Operator.php b/src/Math/Element/Operator.php index 708e604..e91cc55 100644 --- a/src/Math/Element/Operator.php +++ b/src/Math/Element/Operator.php @@ -18,4 +18,4 @@ public function getValue(): string { return $this->value; } -} \ No newline at end of file +} diff --git a/src/Math/Element/Row.php b/src/Math/Element/Row.php index 60b4ad9..cc4722b 100644 --- a/src/Math/Element/Row.php +++ b/src/Math/Element/Row.php @@ -4,4 +4,4 @@ class Row extends AbstractGroupElement { -} \ No newline at end of file +} diff --git a/src/Math/Element/Superscript.php b/src/Math/Element/Superscript.php index 2404d21..11f1a93 100644 --- a/src/Math/Element/Superscript.php +++ b/src/Math/Element/Superscript.php @@ -37,4 +37,4 @@ public function setSuperscript(AbstractElement $element): self return $this; } -} \ No newline at end of file +} diff --git a/src/Math/Math.php b/src/Math/Math.php index ce011a8..962d628 100644 --- a/src/Math/Math.php +++ b/src/Math/Math.php @@ -11,6 +11,7 @@ class Math /** * @param Element\AbstractElement $element + * * @return self */ public function add(Element\AbstractElement $element): self @@ -23,6 +24,7 @@ public function add(Element\AbstractElement $element): self /** * @param Element\AbstractElement $element + * * @return self */ public function remove(Element\AbstractElement $element): self @@ -42,4 +44,4 @@ public function getElements(): array { return $this->elements; } -} \ No newline at end of file +} diff --git a/src/Math/Reader/MathML.php b/src/Math/Reader/MathML.php index 275fe98..eaa348f 100644 --- a/src/Math/Reader/MathML.php +++ b/src/Math/Reader/MathML.php @@ -6,8 +6,8 @@ use DOMElement; use DOMXPath; use Exception; -use PhpOffice\Math\Math; use PhpOffice\Math\Element; +use PhpOffice\Math\Math; class MathML implements ReaderInterface { @@ -66,6 +66,7 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement ->setNumerator($this->getElement($nodeList->item(0))) ->setDenominator($this->getElement($nodeList->item(1))); } + return $element; case 'mi': return new Element\Identifier($nodeValue); @@ -75,13 +76,14 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement if (empty($nodeValue)) { $nodeList = $this->xpath->query('*', $nodeElement); if ( - $nodeList->length == 1 + $nodeList->length == 1 && $nodeList->item(0)->nodeName == 'mchar' && $nodeList->item(0)->hasAttribute('name') ) { $nodeValue = $nodeList->item(0)->getAttribute('name'); } } + return new Element\Operator($nodeValue); case 'mrow': return new Element\Row(); @@ -93,13 +95,14 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement ->setBase($this->getElement($nodeList->item(0))) ->setSuperscript($this->getElement($nodeList->item(1))); } + return $element; - default: + default: throw new Exception(sprintf( '%s : The tag `%s` is not implemented', - __METHOD__, + __METHOD__, $nodeElement->nodeName )); } } -} \ No newline at end of file +} diff --git a/src/Math/Reader/OfficeMathML.php b/src/Math/Reader/OfficeMathML.php index 3a701eb..a6a7ee6 100644 --- a/src/Math/Reader/OfficeMathML.php +++ b/src/Math/Reader/OfficeMathML.php @@ -6,8 +6,8 @@ use DOMElement; use DOMXPath; use Exception; -use PhpOffice\Math\Math; use PhpOffice\Math\Element; +use PhpOffice\Math\Math; class OfficeMathML implements ReaderInterface { @@ -44,8 +44,8 @@ public function read(string $content): ?Math } /** - * @link https://devblogs.microsoft.com/math-in-office/officemath/ - * @link https://learn.microsoft.com/fr-fr/archive/blogs/murrays/mathml-and-ecma-math-omml + * @see https://devblogs.microsoft.com/math-in-office/officemath/ + * @see https://learn.microsoft.com/fr-fr/archive/blogs/murrays/mathml-and-ecma-math-omml */ protected function parseNode(?DOMElement $nodeRowElement, $parent): void { @@ -76,7 +76,7 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement } } // Denominator - $nodeDenominator= $this->xpath->query('m:den/m:r/m:t', $nodeElement); + $nodeDenominator = $this->xpath->query('m:den/m:r/m:t', $nodeElement); if ($nodeDenominator->length == 1) { $value = $nodeDenominator->item(0)->nodeValue; if (is_numeric($value)) { @@ -85,6 +85,7 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement $element->setDenominator(new Element\Identifier($value)); } } + return $element; case 'm:r': $nodeText = $this->xpath->query('m:t', $nodeElement); @@ -96,17 +97,18 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement if (is_numeric($value)) { return new Element\Numeric($value); } + return new Element\Identifier($value); } break; case 'm:oMath': return new Element\Row(); - default: + default: throw new Exception(sprintf( '%s : The tag `%s` is not implemented', - __METHOD__, + __METHOD__, $nodeElement->nodeName )); } } -} \ No newline at end of file +} diff --git a/src/Math/Reader/ReaderInterface.php b/src/Math/Reader/ReaderInterface.php index c7c0c1f..6816c63 100644 --- a/src/Math/Reader/ReaderInterface.php +++ b/src/Math/Reader/ReaderInterface.php @@ -7,4 +7,4 @@ interface ReaderInterface { public function read(string $content): ?Math; -} \ No newline at end of file +} diff --git a/src/Math/Writer/MathML.php b/src/Math/Writer/MathML.php index 85904d3..374131b 100644 --- a/src/Math/Writer/MathML.php +++ b/src/Math/Writer/MathML.php @@ -3,8 +3,8 @@ namespace PhpOffice\Math\Writer; use Exception; -use PhpOffice\Math\Math; use PhpOffice\Math\Element; +use PhpOffice\Math\Math; use XMLWriter; class MathML implements WriterInterface @@ -14,6 +14,7 @@ class MathML implements WriterInterface /** * @param Math $math + * * @return string */ public function write(Math $math): string @@ -44,6 +45,7 @@ protected function writeElementItem(Element\AbstractElement $element): void $this->writeElementItem($childElement); } $this->output->endElement(); + return; } @@ -53,6 +55,7 @@ protected function writeElementItem(Element\AbstractElement $element): void $this->writeElementItem($element->getBase()); $this->writeElementItem($element->getSuperscript()); $this->output->endElement(); + return; } @@ -62,6 +65,7 @@ protected function writeElementItem(Element\AbstractElement $element): void $this->writeElementItem($element->getNumerator()); $this->writeElementItem($element->getDenominator()); $this->output->endElement(); + return; } @@ -80,12 +84,11 @@ protected function getElementTagName(Element\AbstractElement $element): string if ($element instanceof Element\AbstractGroupElement) { throw new Exception(sprintf( '%s : The element of the class `%s` has no tag name', - __METHOD__, + __METHOD__, get_class($element) )); } - // if ($element instanceof Element\Superscript) { return 'msup'; } @@ -101,11 +104,11 @@ protected function getElementTagName(Element\AbstractElement $element): string if ($element instanceof Element\Operator) { return 'mo'; } - + throw new Exception(sprintf( '%s : The element of the class `%s` has no tag name', - __METHOD__, + __METHOD__, get_class($element) )); } -} \ No newline at end of file +} diff --git a/src/Math/Writer/OfficeMathML.php b/src/Math/Writer/OfficeMathML.php index 76ec846..3d986d4 100644 --- a/src/Math/Writer/OfficeMathML.php +++ b/src/Math/Writer/OfficeMathML.php @@ -3,8 +3,8 @@ namespace PhpOffice\Math\Writer; use Exception; -use PhpOffice\Math\Math; use PhpOffice\Math\Element; +use PhpOffice\Math\Math; use XMLWriter; class OfficeMathML implements WriterInterface @@ -14,6 +14,7 @@ class OfficeMathML implements WriterInterface /** * @param Math $math + * * @return string */ public function write(Math $math): string @@ -43,6 +44,7 @@ protected function writeElementItem(Element\AbstractElement $element): void $this->writeElementItem($childElement); } $this->output->endElement(); + return; } @@ -56,6 +58,7 @@ protected function writeElementItem(Element\AbstractElement $element): void $this->writeElementItem($element->getDenominator()); $this->output->endElement(); $this->output->endElement(); + return; } @@ -67,27 +70,25 @@ protected function writeElementItem(Element\AbstractElement $element): void $this->output->endElement(); } - protected function getElementTagName(Element\AbstractElement $element): string { // Group if ($element instanceof Element\AbstractGroupElement) { throw new Exception(sprintf( '%s : The element of the class `%s` has no tag name', - __METHOD__, + __METHOD__, get_class($element) )); } - // if ($element instanceof Element\Fraction) { return 'm:f'; } - + throw new Exception(sprintf( '%s : The element of the class `%s` has no tag name', - __METHOD__, + __METHOD__, get_class($element) )); } -} \ No newline at end of file +} diff --git a/src/Math/Writer/WriterInterface.php b/src/Math/Writer/WriterInterface.php index 3e3a505..5ef5f29 100644 --- a/src/Math/Writer/WriterInterface.php +++ b/src/Math/Writer/WriterInterface.php @@ -7,4 +7,4 @@ interface WriterInterface { public function write(Math $math): string; -} \ No newline at end of file +} diff --git a/tests/Math/Reader/MathMLTest.php b/tests/Math/Reader/MathMLTest.php index 7ea9d60..f0dc326 100644 --- a/tests/Math/Reader/MathMLTest.php +++ b/tests/Math/Reader/MathMLTest.php @@ -4,15 +4,15 @@ namespace Tests\PhpOffice\Math\Reader; -use PHPUnit\Framework\TestCase; use PhpOffice\Math\Element; use PhpOffice\Math\Math; use PhpOffice\Math\Reader\MathML; +use PHPUnit\Framework\TestCase; -class MathMLTest extends TestCase +class MathMLTest extends TestCase { /** - * @covers MathML::read + * @covers \MathML::read */ public function testReadBasic(): void { @@ -70,7 +70,7 @@ public function testReadBasic(): void } /** - * @covers MathML::read + * @covers \MathML::read */ public function testReadFraction(): void { @@ -113,4 +113,4 @@ public function testReadFraction(): void $this->assertInstanceOf(Element\Identifier::class, $subElement->getDenominator()); $this->assertEquals('d', $subElement->getDenominator()->getValue()); } -} \ No newline at end of file +} diff --git a/tests/Math/Reader/OfficeMathMLTest.php b/tests/Math/Reader/OfficeMathMLTest.php index 6cc0696..0d17dd1 100644 --- a/tests/Math/Reader/OfficeMathMLTest.php +++ b/tests/Math/Reader/OfficeMathMLTest.php @@ -4,15 +4,15 @@ namespace Tests\PhpOffice\Math\Reader; -use PHPUnit\Framework\TestCase; use PhpOffice\Math\Element; use PhpOffice\Math\Math; use PhpOffice\Math\Reader\OfficeMathML; +use PHPUnit\Framework\TestCase; -class OfficeMathMLTest extends TestCase +class OfficeMathMLTest extends TestCase { /** - * @covers OfficeMathML::read + * @covers \OfficeMathML::read */ public function testRead(): void { @@ -44,9 +44,8 @@ public function testRead(): void $this->assertEquals(2, $subElements[0]->getDenominator()->getValue()); } - /** - * @covers OfficeMathML::read + * @covers \OfficeMathML::read */ public function testReadWithWTag(): void { @@ -83,29 +82,29 @@ public function testReadWithWTag(): void '; - $reader = new OfficeMathML(); - $math = $reader->read($content); - $this->assertInstanceOf(Math::class, $math); + $reader = new OfficeMathML(); + $math = $reader->read($content); + $this->assertInstanceOf(Math::class, $math); - $elements = $math->getElements(); - $this->assertCount(5, $elements); + $elements = $math->getElements(); + $this->assertCount(5, $elements); - $this->assertInstanceOf(Element\Fraction::class, $elements[0]); - $this->assertInstanceOf(Element\Identifier::class, $elements[0]->getNumerator()); - $this->assertEquals('π', $elements[0]->getNumerator()->getValue()); - $this->assertInstanceOf(Element\Numeric::class, $elements[0]->getDenominator()); - $this->assertEquals(2, $elements[0]->getDenominator()->getValue()); + $this->assertInstanceOf(Element\Fraction::class, $elements[0]); + $this->assertInstanceOf(Element\Identifier::class, $elements[0]->getNumerator()); + $this->assertEquals('π', $elements[0]->getNumerator()->getValue()); + $this->assertInstanceOf(Element\Numeric::class, $elements[0]->getDenominator()); + $this->assertEquals(2, $elements[0]->getDenominator()->getValue()); - $this->assertInstanceOf(Element\Operator::class, $elements[1]); - $this->assertEquals('+', $elements[1]->getValue()); + $this->assertInstanceOf(Element\Operator::class, $elements[1]); + $this->assertEquals('+', $elements[1]->getValue()); - $this->assertInstanceOf(Element\Identifier::class, $elements[2]); - $this->assertEquals('a', $elements[2]->getValue()); + $this->assertInstanceOf(Element\Identifier::class, $elements[2]); + $this->assertEquals('a', $elements[2]->getValue()); - $this->assertInstanceOf(Element\Operator::class, $elements[3]); - $this->assertEquals('∗', $elements[3]->getValue()); + $this->assertInstanceOf(Element\Operator::class, $elements[3]); + $this->assertEquals('∗', $elements[3]->getValue()); - $this->assertInstanceOf(Element\Numeric::class, $elements[4]); - $this->assertEquals(2, $elements[4]->getValue()); + $this->assertInstanceOf(Element\Numeric::class, $elements[4]); + $this->assertEquals(2, $elements[4]->getValue()); } -} \ No newline at end of file +} diff --git a/tests/Math/Writer/MathMLTest.php b/tests/Math/Writer/MathMLTest.php index 0c82b83..6355446 100644 --- a/tests/Math/Writer/MathMLTest.php +++ b/tests/Math/Writer/MathMLTest.php @@ -8,10 +8,10 @@ use PhpOffice\Math\Math; use PhpOffice\Math\Writer\MathML; -class MathMLTest extends WriterTestCase +class MathMLTest extends WriterTestCase { /** - * @covers MathML::write + * @covers \MathML::write */ public function testWrite(): void { @@ -31,18 +31,18 @@ public function testWrite(): void $row->add($superscript); $row->add(new Element\Operator('+')); - + $row->add(new Element\Identifier('b')); $row->add(clone $opTimes); $row->add(new Element\Identifier('x')); - + $row->add(new Element\Operator('+')); $row->add(new Element\Identifier('c')); $writer = new MathML(); $output = $writer->write($math); - + $expected = '' . PHP_EOL . '' @@ -54,8 +54,9 @@ public function testWrite(): void $this->assertEquals($expected, $output); $this->assertIsSchemaMathMLValid($output); } + /** - * @covers OfficeMathML::write + * @covers \OfficeMathML::write */ public function testWriteFraction(): void { @@ -70,7 +71,7 @@ public function testWriteFraction(): void $writer = new MathML(); $output = $writer->write($math); - + $expected = '' . PHP_EOL . '' @@ -83,4 +84,4 @@ public function testWriteFraction(): void $this->assertEquals($expected, $output); $this->assertIsSchemaMathMLValid($output); } -} \ No newline at end of file +} diff --git a/tests/Math/Writer/OfficeMathMLTest.php b/tests/Math/Writer/OfficeMathMLTest.php index 5ba2db3..68d2016 100644 --- a/tests/Math/Writer/OfficeMathMLTest.php +++ b/tests/Math/Writer/OfficeMathMLTest.php @@ -8,10 +8,10 @@ use PhpOffice\Math\Math; use PhpOffice\Math\Writer\OfficeMathML; -class OfficeMathMLTest extends WriterTestCase +class OfficeMathMLTest extends WriterTestCase { /** - * @covers OfficeMathML::write + * @covers \OfficeMathML::write */ public function testWriteFraction(): void { @@ -26,7 +26,7 @@ public function testWriteFraction(): void $writer = new OfficeMathML(); $output = $writer->write($math); - + $expected = '' . '' . '' @@ -37,4 +37,4 @@ public function testWriteFraction(): void . ''; $this->assertEquals($expected, $output); } -} \ No newline at end of file +} diff --git a/tests/Math/Writer/WriterTestCase.php b/tests/Math/Writer/WriterTestCase.php index ef4a715..778fe26 100644 --- a/tests/Math/Writer/WriterTestCase.php +++ b/tests/Math/Writer/WriterTestCase.php @@ -7,7 +7,7 @@ use DOMDocument; use PHPUnit\Framework\TestCase; -class WriterTestCase extends TestCase +class WriterTestCase extends TestCase { public function assertIsSchemaMathMLValid(string $content): void { @@ -74,4 +74,4 @@ protected function failXmlError(LibXMLError $error, string $fileName, string $so $paramStr )); } -} \ No newline at end of file +}