Skip to content

Commit

Permalink
Merge pull request #3 from byjg/php-81
Browse files Browse the repository at this point in the history
Remove deprecation warnings for PHP 8.1. + PSalm
  • Loading branch information
byjg authored Jan 4, 2024
2 parents 9e3bd91 + 86b18ba commit 41297ed
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 82 deletions.
23 changes: 10 additions & 13 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ on:
pull_request:
branches:
- master
schedule:
- cron: "0 8 * * 1"

jobs:
Build:
Expand All @@ -18,24 +16,23 @@ jobs:
strategy:
matrix:
php-version:
- "8.2"
- "8.1"
- "8.0"
- "7.4"
- "7.3"
- "7.2"
- "7.1"

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- run: composer install
- run: ./vendor/bin/phpunit
- run: ./vendor/bin/psalm

Documentation:
runs-on: 'ubuntu-latest'
needs: Build
if: github.ref == 'refs/heads/master'
env:
DOC_GITHUB_TOKEN: '${{ secrets.DOC_TOKEN }}'
steps:
- uses: actions/checkout@v2
- run: curl https://opensource.byjg.com/add-doc.sh | bash /dev/stdin php anydata-text
needs: Build
uses: byjg/byjg.github.io/.github/workflows/add-doc.yaml@master
with:
folder: php
project: ${{ github.event.repository.name }}
secrets: inherit

20 changes: 20 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tasks:
- init: |
echo "No special initialization"
command: |
composer install
image:
file: .gitpod/Dockerfile

vscode:
extensions:
- ikappas.composer
- hbenl.test-adapter-converter
- hbenl.vscode-test-explorer
- felixfbecker.php-debug
- neilbrayfield.php-docblocker
- bmewburn.vscode-intelephense-client
- getpsalm.psalm-vscode-plugin
- SonarSource.sonarlint-vscode
- recca0120.vscode-phpunit
4 changes: 4 additions & 0 deletions .gitpod/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# You can find the new timestamped tags here: https://hub.docker.com/r/gitpod/workspace-full/tags
FROM gitpod/workspace-full:latest

RUN sudo install-packages php-xdebug
42 changes: 31 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ See more about Anydataset [here](https://opensource.byjg.com/anydataset).
This type of files uses a delimiter to define each field. The most common formart is CSV but you can use your own based on a regular expression.
The class TextFileIterator has three constants with pre-defined formats:

- TextFileDataset::CSVFILE - A generic file definition. It accept both `,` and `;` as delimiter.
- TextFileDataset::CSVFILE_COMMA - The CSV file. It accept only `,` as delimiter.
- TextFileDataset::CSVFILE_SEMICOLON - A CSV variation. It accept only `;` as delimiter.
- TextFileDataset::CSVFILE - A generic file definition. It accept both `,` and `;` as delimiter.
- TextFileDataset::CSVFILE_COMMA - The CSV file. It accept only `,` as delimiter.
- TextFileDataset::CSVFILE_SEMICOLON - A CSV variation. It accept only `;` as delimiter.

example1.csv
```csv
Joao;Magalhaes
John;Doe
Jane;Smith
```
example1.php
```php
<?php
$file = "Joao;Magalhaes
John;Doe
Jane;Smith";
$file = file_get_contents("example1.csv");

$dataset = \ByJG\AnyDataset\Text\TextFileDataset::getInstance($file)
->withFields(["name", "surname"])
Expand All @@ -40,11 +45,17 @@ foreach ($iterator as $row) {

### Text File Delimited (CSV) - Get field names from first line

example2.csv
```csv
firstname;lastname
John;Doe
Jane;Smith
```

example2.php
```php
<?php
$file = "firstname;lastname
John;Doe
Jane;Smith";
$file = file_get_contents("example2.csv");

// If omit `withFields` will get the field names from first line of the file
$dataset = \ByJG\AnyDataset\Text\TextFileDataset::getInstance($file)
Expand Down Expand Up @@ -190,13 +201,22 @@ $formatter->toText();

## Install

Just type: `composer require "byjg/anydataset-text=4.2.*"`
```
composer require "byjg/anydataset-text"
```

## Running Unit tests

```bash
vendor/bin/phpunit
```

## Dependencies

```mermaid
flowchart TD
byjg/anydataset-text --> byjg/anydataset
```

----
[Open source ByJG](http://opensource.byjg.com)
[Open source ByJG](http://opensource.byjg.com)
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": ">=5.6.0",
"byjg/anydataset": "4.1.*"
"php": ">=7.4",
"byjg/anydataset": "4.9.*"
},
"require-dev": {
"phpunit/phpunit": "5.7.*|7.4.*|^9.5"
"phpunit/phpunit": "5.7.*|7.4.*|^9.5",
"vimeo/psalm": "^4.24"
},
"license": "MIT"
}
5 changes: 4 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ and open the template in the editor.

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
colors="false"
colors="true"
testdox="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
stopOnFailure="false">


<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
Expand Down
15 changes: 15 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
16 changes: 15 additions & 1 deletion src/Enum/FixedTextDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ class FixedTextDefinition
const TYPE_STRING = "string";
const TYPE_NUMBER = "number";

/** @var string */
public $fieldName;

/** @var int */
public $startPos;

/** @var int */
public $length;

/** @var array|null */
public $requiredValue;

/** @var string */
public $type;

/** @var array|null */
public $subTypes = array();

/**
Expand All @@ -25,7 +36,7 @@ class FixedTextDefinition
* @param int $startPos
* @param int $length
* @param string $type
* @param array $requiredValue
* @param array|null $requiredValue
* @param FixedTextDefinition[] $subTypes
*/
public function __construct($fieldName, $startPos, $length, $type = "string", $requiredValue = null, $subTypes = null)
Expand All @@ -37,6 +48,9 @@ public function __construct($fieldName, $startPos, $length, $type = "string", $r
$this->requiredValue = $requiredValue;
$this->subTypes = $subTypes;

/**
* @psalm-suppress DocblockTypeContradiction
*/
if (!empty($this->requiredValue) && !is_array($this->requiredValue)) {
throw new InvalidArgumentException("Required Value must be empty or an ARRAY of values");
}
Expand Down
14 changes: 12 additions & 2 deletions src/FixedTextFileDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
class FixedTextFileDataset
{

/**
* @var string
*/
protected $source;

/**
* @var FixedTextDefinition[]
* @psalm-suppress PossiblyNullPropertyAssignmentValue
*/
protected $fieldDefinition = null;

/**
* @var string
*/
protected $fieldDefinition;
protected $sourceType;

/**
Expand Down Expand Up @@ -57,7 +65,9 @@ public static function getInstance($source)
*/
public function withFieldDefinition($fieldDefinition)
{

/**
* @psalm-suppress DocblockTypeContradiction
*/
if (!is_array($fieldDefinition)) {
throw new InvalidArgumentException("You must define an array of FixedTextDefinition class.");
}
Expand Down
Loading

0 comments on commit 41297ed

Please sign in to comment.