Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions .atoum.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php

use \mageekguy\atoum;
use atoum\atoum\reports;
use atoum\atoum\reports\coverage;
use atoum\atoum\writers\std;

$report = $script->addDefaultReport();

$coverageField = new atoum\report\fields\runner\coverage\html('Pattern Matching', './reports/');
$report->addField($coverageField);

$cloverWriter = new atoum\writers\file('./reports/atoum.coverage.xml');
$cloverReport = new atoum\reports\asynchronous\clover();
$cloverReport->addWriter($cloverWriter);
$runner->addReport($cloverReport);
$script->addDefaultReport();

$clover = new \atoum\atoum\reports\sonar\clover();
$writer = new \atoum\atoum\writers\file('coverage.xml');
$clover->addWriter($writer);
$runner->addReport($clover);
$runner->addTestsFromDirectory('./tests');
35 changes: 35 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: pattern-matching CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ['7.2', '7.3', '7.4', '8.0']
name: PHP ${{ matrix.php }}
steps:
- uses: actions/checkout@v1
- name: Install PHP
uses: shivammathur/setup-php@master
with:
php-version: ${{ matrix.php }}
# install xdebug for code coverage purposes
extensions: xdebug
- name: Validate composer.json and composer.lock
run: composer validate
- name: Get Composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ matrix.php }}-composer-
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run test suite
run: vendor/bin/atoum --enable-branch-and-path-coverage
- name: Generate codecov coverage report
run: bash <(curl -s https://codecov.io/bash)
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor/
reports/
composer.lock
*.cache
16 changes: 16 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = Finder::create()
->exclude(['vendor', 'cache', 'bin'])
->in(__DIR__);

$config = new Config;

return $config
->setRules([
'@PSR12' => true
])
->setFinder($finder);
8 changes: 8 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
risky: false
version: 7
preset: 'psr12'
finder:
exclude:
- 'node_modules'
- 'vendor'
name: '*.php'
23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Pattern Matching

[![Build Status](https://travis-ci.org/functional-php/pattern-matching.svg)](https://travis-ci.org/functional-php/pattern-matching)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/functional-php/pattern-matching/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/functional-php/pattern-matching/?branch=master)
[![pattern-matching CI](https://github.com/ace411/pattern-matching/actions/workflows/php.yml/badge.svg?branch=master)](https://github.com/ace411/pattern-matching/actions/workflows/php.yml)
[![StyleCI](https://github.styleci.io/repos/341440518/shield?branch=master)](https://github.styleci.io/repos/341440518?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/functional-php/pattern-matching/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/functional-php/pattern-matching/?branch=master)
[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/functional-php/pattern-matching.svg)](http://isitmaintained.com/project/functional-php/pattern-matching "Average time to resolve an issue")
[![Percentage of issues still open](http://isitmaintained.com/badge/open/functional-php/pattern-matching.svg)](http://isitmaintained.com/project/functional-php/pattern-matching "Percentage of issues still open")
[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/functional-php/pattern-matching.svg)](http://isitmaintained.com/project/functional-php/pattern-matching 'Average time to resolve an issue')
[![Percentage of issues still open](http://isitmaintained.com/badge/open/functional-php/pattern-matching.svg)](http://isitmaintained.com/project/functional-php/pattern-matching 'Percentage of issues still open')
[![Chat on Gitter](https://img.shields.io/gitter/room/gitterHQ/gitter.svg)](https://gitter.im/functional-php)

Pattern matching is the process of checking a series of token against a pattern.
It is different from pattern recognition as the match needs to be exact.
The process does not only match as a switch statement does, it also assigns the value
a bit like the ``list`` construct in PHP, a process called **destructuring**.
a bit like the `list` construct in PHP, a process called **destructuring**.

Most functional languages implement it as a core feature. Here is are some small examples in Haskell:

``` haskell
```haskell

fact :: (Integral a) => a -> a
fact 0 = 1
Expand Down Expand Up @@ -74,15 +74,15 @@ You can also use the `match` function if you want to have a beefed up version of
use FunctionalPHP\PatternMatching as m;

function factorial($n) {
return m\match($n, [
return m\pmatch($n, [
'0' => 1,
'n' => function($n) use(&$fact) {
return $n * factorial($n - 1);
}
]);
}

echo m\match([1, 2, ['a', 'b'], true], [
echo m\pmatch([1, 2, ['a', 'b'], true], [
'"toto"' => 'first',
'[a, [b, c], d]' => 'second',
'[a, _, (x:xs), c]' => 'third',
Expand All @@ -94,7 +94,7 @@ echo m\match([1, 2, ['a', 'b'], true], [

If you are just interested in destructuring your values, there is also a helper for that:

``` php
```php

use FunctionalPHP\PatternMatching as m;

Expand All @@ -112,21 +112,21 @@ print_r(m\extract([1, 2, ['a', 'b'], true], '[a, _, (x:xs), c]'));

Here is a quick recap of the available patterns:

| Name | Format | Example |
|---------------|-----------------------------------|---------------------------------|
| Constant | Any scalar value (int, float, string, boolean) | ``1.0``, ``42``, "test" |
| Variable | ``identifier`` | ``a``, ``name``, ``anything`` |
| Array | ``[<pattern>, ..., <pattern>]`` | ``[]``, ``[a]``, ``[a, b, c]`` |
| Cons | ``(identifier:list-identifier)`` | ``(x:xs)``, ``(x:y:z:xs)`` |
| Wildcard | ``_`` | ``_`` |
| As | ``identifier@(<pattern>)`` | ``all@(x:xs)`` |
| Name | Format | Example |
| -------- | ---------------------------------------------- | ------------------------ |
| Constant | Any scalar value (int, float, string, boolean) | `1.0`, `42`, "test" |
| Variable | `identifier` | `a`, `name`, `anything` |
| Array | `[<pattern>, ..., <pattern>]` | `[]`, `[a]`, `[a, b, c]` |
| Cons | `(identifier:list-identifier)` | `(x:xs)`, `(x:y:z:xs)` |
| Wildcard | `_` | `_` |
| As | `identifier@(<pattern>)` | `all@(x:xs)` |

## Testing

You can run the test suite for the library using:

composer test

A test report will be available in the `reports` directory.

## Contributing
Expand Down
18 changes: 13 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"name": "functional-php/pattern-matching",
"description": "Pattern matching for PHP with automatic destructuring.",
"keywords": ["functional", "pattern", "pattern matching", "destructuring"],
"keywords": [
"functional",
"pattern",
"pattern matching",
"destructuring"
],
"license": "BSD-3-Clause",
"authors": [
{
Expand All @@ -13,10 +18,9 @@
"php": ">=5.6.0"
},
"require-dev": {
"atoum/atoum": "*"
},
"scripts": {
"test": "./vendor/bin/atoum --enable-branch-and-path-coverage"
"atoum/atoum": "^3 || ^4",
"atoum/reports-extension": "^3 || ^4",
"friendsofphp/php-cs-fixer": "~2 || ~3"
},
"autoload": {
"psr-4": {
Expand All @@ -25,5 +29,9 @@
"files": [
"src/functions.php"
]
},
"scripts": {
"ci:fix": "./vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff --verbose --rules=@PSR12",
"test": "./vendor/bin/atoum --enable-branch-and-path-coverage"
}
}
101 changes: 0 additions & 101 deletions composer.lock

This file was deleted.

Loading