Skip to content

Commit

Permalink
Github Actions : PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Sep 21, 2023
1 parent a597340 commit a39ef41
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 99 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ jobs:
- name: Run PHPCSFixer
run: php-cs-fixer fix --dry-run --diff

phpstan:
name: PHP Static Analysis
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: xml

- uses: actions/checkout@v2

- name: Composer Install
run: composer install --ansi --prefer-dist --no-interaction --no-progress

- name: Run phpstan
run: ./vendor/bin/phpstan analyse -c phpstan.neon.dist

phpunit:
name: PHPUnit
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"friendsofphp/php-cs-fixer": "^2.1"
},
"require-dev": {
"phpunit/phpunit": ">=7.0"
"phpunit/phpunit": ">=7.0",
"phpstan/phpstan": "^0.12.88 || ^1.0.0"
}
}
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 7
bootstrapFiles:
- vendor/autoload.php
paths:
- src
- tests
reportUnmatchedIgnoredErrors: false
ignoreErrors:

## Remove after remove ArrayObject
treatPhpDocTypesAsCertain: false
10 changes: 9 additions & 1 deletion src/Math/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@

namespace PhpOffice\Math\Element;

use PhpOffice\Math\Math;

abstract class AbstractElement
{
/**
* @var string
* @var Math|AbstractGroupElement|null
*/
protected $parent;

/**
* @param Math|AbstractGroupElement|null $parent
*/
public function setParent($parent): self
{
$this->parent = $parent;

return $this;
}

/**
* @return Math|AbstractGroupElement|null
*/
public function getParent()
{
return $this->parent;
Expand Down
4 changes: 3 additions & 1 deletion src/Math/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpOffice\Math;

use PhpOffice\Math\Element\AbstractElement;

class Math
{
/**
Expand Down Expand Up @@ -32,7 +34,7 @@ public function remove(Element\AbstractElement $element): self
$this->elements = array_filter($this->elements, function ($child) use ($element) {
return $child != $element;
});
$component->setParent(null);
$element->setParent(null);

return $this;
}
Expand Down
20 changes: 13 additions & 7 deletions src/Math/Reader/MathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DOMDocument;
use DOMElement;
use DOMNode;
use DOMXPath;
use Exception;
use PhpOffice\Math\Element;
Expand Down Expand Up @@ -41,10 +42,13 @@ public function read(string $content): ?Math
return $this->math;
}

protected function parseNode(?DOMElement $nodeRowElement, $parent): void
/**
* @param Math|Element\AbstractGroupElement $parent
*/
protected function parseNode(?DOMNode $nodeRowElement, $parent): void
{
$this->xpath = new DOMXpath($this->dom);
foreach ($this->xpath->query('*', $nodeRowElement) as $nodeElement) {
foreach ($this->xpath->query('*', $nodeRowElement) ?: [] as $nodeElement) {
$element = $this->getElement($nodeElement);
$parent->add($element);

Expand All @@ -54,14 +58,14 @@ protected function parseNode(?DOMElement $nodeRowElement, $parent): void
}
}

protected function getElement(DOMElement $nodeElement): Element\AbstractElement
protected function getElement(DOMNode $nodeElement): Element\AbstractElement
{
$nodeValue = trim($nodeElement->nodeValue);
switch ($nodeElement->nodeName) {
case 'mfrac':
$element = new Element\Fraction();
$nodeList = $this->xpath->query('*', $nodeElement);
if ($nodeList->length == 2) {
if ($nodeList && $nodeList->length == 2) {
$element
->setNumerator($this->getElement($nodeList->item(0)))
->setDenominator($this->getElement($nodeList->item(1)));
Expand All @@ -71,13 +75,15 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement
case 'mi':
return new Element\Identifier($nodeValue);
case 'mn':
return new Element\Numeric($nodeValue);
return new Element\Numeric(floatval($nodeValue));
case 'mo':
if (empty($nodeValue)) {
$nodeList = $this->xpath->query('*', $nodeElement);
if (
$nodeList->length == 1
$nodeList
&& $nodeList->length == 1
&& $nodeList->item(0)->nodeName == 'mchar'
&& $nodeList->item(0) instanceof DOMElement
&& $nodeList->item(0)->hasAttribute('name')
) {
$nodeValue = $nodeList->item(0)->getAttribute('name');
Expand All @@ -90,7 +96,7 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement
case 'msup':
$element = new Element\Superscript();
$nodeList = $this->xpath->query('*', $nodeElement);
if ($nodeList->length == 2) {
if ($nodeList && $nodeList->length == 2) {
$element
->setBase($this->getElement($nodeList->item(0)))
->setSuperscript($this->getElement($nodeList->item(1)));
Expand Down
31 changes: 17 additions & 14 deletions src/Math/Reader/OfficeMathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
namespace PhpOffice\Math\Reader;

use DOMDocument;
use DOMElement;
use DOMNode;
use DOMXPath;
use Exception;
use PhpOffice\Math\Element;
use PhpOffice\Math\Math;

class OfficeMathML implements ReaderInterface
{
/** @var DOMDocument */
protected $dom;

/** @var Math */
protected $math;

/** @var XMLReader */
protected $xmlReader;

/** @var DOMXpath */
protected $xpath;

Expand Down Expand Up @@ -46,11 +46,13 @@ public function read(string $content): ?Math
/**
* @see https://devblogs.microsoft.com/math-in-office/officemath/
* @see https://learn.microsoft.com/fr-fr/archive/blogs/murrays/mathml-and-ecma-math-omml
*
* @param Math|Element\AbstractGroupElement $parent
*/
protected function parseNode(?DOMElement $nodeRowElement, $parent): void
protected function parseNode(?DOMNode $nodeRowElement, $parent): void
{
$this->xpath = new DOMXpath($this->dom);
foreach ($this->xpath->query('*', $nodeRowElement) as $nodeElement) {
foreach ($this->xpath->query('*', $nodeRowElement) ?: [] as $nodeElement) {
$element = $this->getElement($nodeElement);
$parent->add($element);

Expand All @@ -60,27 +62,27 @@ protected function parseNode(?DOMElement $nodeRowElement, $parent): void
}
}

protected function getElement(DOMElement $nodeElement): Element\AbstractElement
protected function getElement(DOMNode $nodeElement): Element\AbstractElement
{
switch ($nodeElement->nodeName) {
case 'm:f':
$element = new Element\Fraction();
// Numerator
$nodeNumerator = $this->xpath->query('m:num/m:r/m:t', $nodeElement);
if ($nodeNumerator->length == 1) {
if ($nodeNumerator && $nodeNumerator->length == 1) {
$value = $nodeNumerator->item(0)->nodeValue;
if (is_numeric($value)) {
$element->setNumerator(new Element\Numeric($value));
$element->setNumerator(new Element\Numeric(floatval($value)));
} else {
$element->setNumerator(new Element\Identifier($value));
}
}
// Denominator
$nodeDenominator = $this->xpath->query('m:den/m:r/m:t', $nodeElement);
if ($nodeDenominator->length == 1) {
if ($nodeDenominator && $nodeDenominator->length == 1) {
$value = $nodeDenominator->item(0)->nodeValue;
if (is_numeric($value)) {
$element->setDenominator(new Element\Numeric($value));
$element->setDenominator(new Element\Numeric(floatval($value)));
} else {
$element->setDenominator(new Element\Identifier($value));
}
Expand All @@ -89,18 +91,19 @@ protected function getElement(DOMElement $nodeElement): Element\AbstractElement
return $element;
case 'm:r':
$nodeText = $this->xpath->query('m:t', $nodeElement);
if ($nodeText->length == 1) {
if ($nodeText && $nodeText->length == 1) {
$value = trim($nodeText->item(0)->nodeValue);
if (in_array($value, $this->operators)) {
return new Element\Operator($value);
}
if (is_numeric($value)) {
return new Element\Numeric($value);
return new Element\Numeric(floatval($value));
}

return new Element\Identifier($value);
}
break;

return new Element\Identifier('');
case 'm:oMath':
return new Element\Row();
default:
Expand Down
19 changes: 15 additions & 4 deletions src/Math/Writer/MathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,21 @@ protected function writeElementItem(Element\AbstractElement $element): void
return;
}

// Element\AbstractElement
$this->output->startElement($this->getElementTagName($element));
$this->output->text((string) $element->getValue());
$this->output->endElement();
if ($element instanceof Element\Identifier
|| $element instanceof Element\Numeric
|| $element instanceof Element\Operator) {
$this->output->startElement($this->getElementTagName($element));
$this->output->text((string) $element->getValue());
$this->output->endElement();

return;
}

throw new Exception(sprintf(
'%s : The class `%s` is not implemented',
__METHOD__,
get_class($element)
));
}

protected function getElementTagName(Element\AbstractElement $element): string
Expand Down
23 changes: 17 additions & 6 deletions src/Math/Writer/OfficeMathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,23 @@ protected function writeElementItem(Element\AbstractElement $element): void
return;
}

// Element\AbstractElement
$this->output->startElement('m:r');
$this->output->startElement('m:t');
$this->output->text((string) $element->getValue());
$this->output->endElement();
$this->output->endElement();
if ($element instanceof Element\Identifier
|| $element instanceof Element\Numeric
|| $element instanceof Element\Operator) {
$this->output->startElement('m:r');
$this->output->startElement('m:t');
$this->output->text((string) $element->getValue());
$this->output->endElement();
$this->output->endElement();

return;
}

throw new Exception(sprintf(
'%s : The class `%s` is not implemented',
__METHOD__,
get_class($element)
));
}

protected function getElementTagName(Element\AbstractElement $element): string
Expand Down
Loading

0 comments on commit a39ef41

Please sign in to comment.