Skip to content

Commit

Permalink
Merge pull request #3 from eclipxe13/development
Browse files Browse the repository at this point in the history
Versión 0.2.0
  • Loading branch information
eclipxe13 authored Mar 23, 2021
2 parents 80a3e83 + 93e5aa7 commit d8bfe12
Show file tree
Hide file tree
Showing 24 changed files with 393 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
* text=auto

# Do not put this files on a distribution package (by .gitignore)
/tools/ export-ignore
/vendor/ export-ignore
/composer.lock export-ignore
.phpunit.result.cache export-ignore

# Do not put this files on a distribution package
/build/ export-ignore
/tests/ export-ignore
/.phive/ export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs.dist export-ignore
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# do not include this files on git
/vendor
/tools/
/vendor/
/composer.lock
.phpunit.result.cache
8 changes: 8 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="php-cs-fixer" version="^2.4" location="./tools/php-cs-fixer" copy="false"/>
<phar name="phpcbf" version="^3.0" location="./tools/phpcbf" copy="false"/>
<phar name="phpcs" version="^3.0" location="./tools/phpcs" copy="false"/>
<phar name="phpstan" version="^0.12" location="./tools/phpstan" copy="false"/>
<phar name="infection" version="^0.21.0" location="./tools/infection" copy="false"/>
</phive>
2 changes: 1 addition & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ return PhpCsFixer\Config::create()
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude(['vendor', 'build'])
->exclude(['tools', 'vendor', 'build'])
)
;
11 changes: 5 additions & 6 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ filter:
build:
dependencies:
override:
- composer self-update --no-interaction --no-progress
- composer remove squizlabs/php_codesniffer friendsofphp/php-cs-fixer phpstan/phpstan --dev --no-interaction --no-progress --no-update
- composer install --no-interaction --prefer-dist
- composer update --no-interaction --prefer-dist
nodes:
analysis: # see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/
project_setup: {override: true}
analysis: # see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/
project_setup:
override: true
tests:
override:
- php-scrutinizer-run --enable-security-analysis
- command: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
- command: vendor/bin/phpunit --verbose --testdox --coverage-clover=coverage.clover
coverage:
file: coverage.clover
format: clover
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ php: ["7.3", "7.4", "8.0"]
cache:
- directories:
- $HOME/.composer

env:
global:
- PHP_CS_FIXER_IGNORE_ENV=yes
- $HOME/.phive

before_script:
- phpenv config-rm xdebug.ini || true
- travis_retry composer self-update --no-interaction --2
- travis_retry composer upgrade --no-interaction --prefer-dist
- wget -O phive https://phar.io/releases/phive.phar && chmod +x phive
- ./phive install --force-accept-unsigned --trust-gpg-keys 0x4AA394086372C20A,0x31C7E470E2138192,0xE82B2FB314E9906E,0xCF1A108D0E7AE720,0xC5095986493B4AA0

script:
- vendor/bin/php-cs-fixer fix --dry-run --verbose
- vendor/bin/phpcbf --colors -sp src/ tests/ bin/
- PHP_CS_FIXER_IGNORE_ENV=yes tools/php-cs-fixer fix --dry-run --verbose
- tools/phpcs --colors -sp src/ tests/ bin/
- vendor/bin/phpunit --testdox --verbose
- vendor/bin/phpstan analyse --no-progress --verbose --level max src/ tests/ bin/
- tools/phpstan analyse --no-progress --verbose --level max src/ tests/ bin/
- bash bin/check-current-max-occurs-paths.bash

notifications:
email:
Expand Down
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,15 @@ Considera las siguientes directrices:
## Proceso de construcción

```shell
# Instala phive, sigue las indicaciones seguras de https://phar.io/#Install o la forma insegura:
wget https://phar.io/releases/phive.phar -O ~/.local/bin/phive

## Si no se han instalado las herramientas previamente
phive install --force-accept-unsigned --trust-gpg-keys 0x4AA394086372C20A,0x31C7E470E2138192,0xE82B2FB314E9906E,0xCF1A108D0E7AE720,0xC5095986493B4AA0

# Actualiza tus dependencias
composer update
phive update

# Verificación de estilo de código
composer dev:check-style
Expand All @@ -76,6 +83,9 @@ composer dev:test

# Ejecución todo en uno, corregir estilo, verificar estilo y correr pruebas
composer dev:build

# Opcional: correr las pruebas de mutación
composer dev:infection
```

[phpCfdi]: https://github.com/phpcfdi/
Expand Down
13 changes: 13 additions & 0 deletions bin/check-current-max-occurs-paths.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash -e

TEMPFILE="$(mktemp)"
BINPATH="$(dirname $0)"

echo "Creating UnboundedOccursPaths.json on $TEMPFILE"
php "${BINPATH}/max-occurs-paths.php" > "$TEMPFILE"

echo "Comparing to current UnboundedOccursPaths.json"
diff -u -b -B "${BINPATH}/../src/UnboundedOccursPaths.json" "$TEMPFILE"

echo "OK: Files match"
rm "$TEMPFILE"
27 changes: 14 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"squizlabs/php_codesniffer": "^3.0",
"friendsofphp/php-cs-fixer": "^2.4",
"phpstan/phpstan": "^0.12"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand All @@ -35,26 +32,30 @@
"scripts": {
"dev:build": ["@dev:fix-style", "@dev:check-style", "@dev:test"],
"dev:check-style": [
"vendor/bin/php-cs-fixer fix --dry-run --verbose",
"vendor/bin/phpcs --colors -sp src/ tests/ bin/"
"@php tools/php-cs-fixer fix --dry-run --verbose",
"@php tools/phpcs --colors -sp src/ tests/ bin/"
],
"dev:fix-style": [
"vendor/bin/php-cs-fixer fix --verbose",
"vendor/bin/phpcbf --colors -sp src/ tests/ bin/"
"@php tools/php-cs-fixer fix --verbose",
"@php tools/phpcbf --colors -sp src/ tests/ bin/"
],
"dev:test": [
"vendor/bin/phpunit --testdox --verbose --stop-on-failure",
"vendor/bin/phpstan analyse --verbose --level max src/ tests/ bin/"
"@php vendor/bin/phpunit --testdox --verbose --stop-on-failure",
"@php tools/phpstan analyse --verbose --level max src/ tests/ bin/"
],
"dev:coverage": [
"@php -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-html build/coverage/html/"
"@php -dzend_extension=xdebug.so -dxdebug.mode=coverage vendor/bin/phpunit --coverage-xml build/coverage/xml/ --coverage-html build/coverage/html/"
],
"dev:infection": [
"@php tools/infection --show-mutations --no-progress"
]
},
"scripts-descriptions": {
"dev:build": "DEV: run dev:fix-style and dev:tests, run before pull request",
"dev:check-style": "DEV: search for code style errors using php-cs-fixer and phpcs",
"dev:fix-style": "DEV: fix code style errors using php-cs-fixer and phpcbf",
"dev:test": "DEV: run phpunit and phpstan",
"dev:coverage": "DEV: run phpunit with xdebug and storage coverage in build/coverage/html/"
"dev:test": "DEV: run dev:fix-style, phpunit and phpstan",
"dev:coverage": "DEV: run phpunit with xdebug and storage coverage in build/coverage/html/",
"dev:infection": "DEV: run mutation tests using infection"
}
}
9 changes: 9 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ versión aunque sí su incorporación en la rama principal de trabajo, generalme

## Listado de cambios

### Versión 0.2.0 2021-03-22

- Se extrae la lógica del conteo de hijos de `Nodes\Children` a `Nodes\KeysCounter`.
- Se corrigen los test y las llamadas de `file_get_contents`.
- Conseguir el 100% de testeo.
- Agregar a Travis-CI la comprobación de que el archivo `src/UnboundedOccursPaths.json` no ha cambiado.
- Usar `phive` para las herramientas de desarrollo.
- Se agrega `infection` para correr pruebas de mutación. No es mandatorio por el momento.

### Versión 0.1.0 2021-02-02 ¡Feliz cumpleaños Dany!

- Primera liberación para su uso público.
4 changes: 1 addition & 3 deletions docs/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

## Ideas, por favor, levante un ticket para discutirlas

- Agregar a Travis la comprobación del archivo `src/UnboundedOccursPaths.json`

- ¿Los elementos `Comprobante/Complemento` deberían colapsarse?

- Usar infection
- Hacer que `infection` se ejecute con un mínimo requerido en la integración contínua.
14 changes: 14 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "build\/infection.log"
},
"mutators": {
"@default": true
},
"initialTestsPhpOptions": "-dzend_extension=xdebug.so -dxdebug.mode=coverage"
}
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<arg name="encoding" value="utf-8"/>
<arg name="report-width" value="auto"/>
<arg name="extensions" value="php"/>
<arg name="cache" value="build/phpcs.cache"/>
<rule ref="PSR2"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Generic.CodeAnalysis.EmptyStatement"/>
Expand Down
9 changes: 7 additions & 2 deletions src/CfdiToDataNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ final class CfdiToDataNode
/** @var UnboundedOccursPaths */
private $unboundedOccursPaths;

public function __construct(UnboundedOccursPaths $multipleChildrenPaths)
public function __construct(UnboundedOccursPaths $unboundedOccursPaths)
{
$this->unboundedOccursPaths = $multipleChildrenPaths;
$this->unboundedOccursPaths = $unboundedOccursPaths;
}

public function getUnboundedOccursPaths(): UnboundedOccursPaths
{
return $this->unboundedOccursPaths;
}

public function convertXmlContent(string $xmlContents): Nodes\Node
Expand Down
14 changes: 5 additions & 9 deletions src/Nodes/Children.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,27 @@ final class Children
/** @var UnboundedOccursPaths */
private $unboundedOccursPaths;

/** @var array<string, int> */
private $childrenCountByKey = [];
/** @var KeysCounter */
private $keysCounter;

public function __construct(UnboundedOccursPaths $unboundedOccursPaths)
{
$this->unboundedOccursPaths = $unboundedOccursPaths;
$this->keysCounter = new KeysCounter();
}

public function append(Node $child): void
{
$this->children[] = $child;
$this->childrenCountByKey[$child->getKey()] = $this->getChildrenCountByKey($child->getKey());
$this->keysCounter->register($child->getKey());
}

public function isChildrenMultiple(Node $child): bool
{
return ($this->getChildrenCountByKey($child->getKey()) > 1)
return $this->keysCounter->hasMany($child->getKey())
|| $this->unboundedOccursPaths->match($child->getPath());
}

private function getChildrenCountByKey(string $key): int
{
return $this->childrenCountByKey[$key] ?? 0;
}

/** @return array<string, string|array> */
public function toArray(): array
{
Expand Down
26 changes: 26 additions & 0 deletions src/Nodes/KeysCounter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace PhpCfdi\CfdiToJson\Nodes;

final class KeysCounter
{
/** @var array<string, int> */
private $counts;

public function register(string $key): void
{
$this->counts[$key] = $this->get($key) + 1;
}

public function get(string $key): int
{
return $this->counts[$key] ?? 0;
}

public function hasMany(string $key): bool
{
return $this->get($key) > 1;
}
}
8 changes: 2 additions & 6 deletions src/XsdMaxOccurs/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
namespace PhpCfdi\CfdiToJson\XsdMaxOccurs;

use RuntimeException;
use Throwable;

final class Downloader implements DownloaderInterface
{
public function get(string $url): string
{
try {
$contents = file_get_contents($url);
} catch (Throwable $exception) {
throw new RuntimeException("Unable to get $url contents", 0, $exception);
}
/** @noinspection PhpUsageOfSilenceOperatorInspection */
$contents = @file_get_contents($url);
if (false === $contents) {
throw new RuntimeException("Unable to get $url contents");
}
Expand Down
8 changes: 4 additions & 4 deletions src/XsdMaxOccurs/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function obtainPathsFromXsdContents(string $xsdContents): array
$document->loadXML($xsdContents);

return array_merge(
$this->obtainPathsForXPathQuery($document, '//xs:element[@maxOccurs="unbounded"]'),
$this->obtainPathsForXPathQuery($document, '//xs:sequence[@maxOccurs="unbounded"]/xs:element'),
$this->obtainPathsForXPathQuery($document, '//xs:choice[@maxOccurs="unbounded"]/xs:element'),
$this->obtainPathsForXPathQuery($document, '//x:element[@maxOccurs="unbounded"]'),
$this->obtainPathsForXPathQuery($document, '//x:sequence[@maxOccurs="unbounded"]/x:element'),
$this->obtainPathsForXPathQuery($document, '//x:choice[@maxOccurs="unbounded"]/x:element'),
);
}

Expand All @@ -32,7 +32,7 @@ private function obtainPathsForXPathQuery(DOMDocument $document, string $query):
{
$paths = [];
$xpath = new DOMXPath($document);
$xpath->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');
$xpath->registerNamespace('x', 'http://www.w3.org/2001/XMLSchema');
$nodes = $xpath->query($query) ?: new DOMNodeList();
foreach ($nodes as $node) {
if ($node instanceof DOMElement) {
Expand Down
4 changes: 3 additions & 1 deletion tests/Functional/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ public function testConverterExportsNodesAsArrayWhenTheyAreKnownFromComplemento(
public function testJsonConverter(): void
{
$xmlContents = $this->fileContents('cfdi-example.xml');
$jsonFile = $this->filePath('cfdi-example.json');
/** @noinspection PhpUnhandledExceptionInspection */
$json = JsonConverter::convertToJson($xmlContents);
$this->assertJsonStringEqualsJsonString(json_encode($this->data) ?: '', $json);
$this->assertJsonStringEqualsJsonFile($jsonFile, $json);
$this->assertStringEqualsFile($jsonFile, $json, 'Check that the default format is preserved');
}
}
Loading

0 comments on commit d8bfe12

Please sign in to comment.