Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Nov 24, 2023
0 parents commit b2a7ad3
Show file tree
Hide file tree
Showing 23 changed files with 9,103 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.github/ export-ignore
/stubs/ export-ignore
/tests/ export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php-cs-fixer.dist.php export-ignore
/composer.lock export-ignore
/infection.json5.dist export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml.dist export-ignore
162 changes: 162 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
name: Continuous Integration

on:
pull_request: ~
push:
branches: ['*.*.x']

jobs:
composer-validate:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
- run: composer validate --strict --ansi

check-require:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
- uses: ramsey/composer-install@v2
with:
composer-options: --optimize-autoloader
- run: composer check-require

check-unused:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
- uses: ramsey/composer-install@v2
with:
composer-options: --optimize-autoloader
- run: composer check-unused

composer-normalize:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
- uses: ramsey/composer-install@v2
with:
composer-options: --optimize-autoloader
- run: composer normalize --dry-run --diff --ansi

php-cs-fixer:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2, cs2pr
coverage: none
- uses: ramsey/composer-install@v2
with:
composer-options: --optimize-autoloader
- run: composer fixcs -- --dry-run --diff --format=checkstyle --ansi | cs2pr

rector:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
- uses: ramsey/composer-install@v2
with:
composer-options: --optimize-autoloader
- run: composer rector -- --dry-run

psalm:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
- uses: ramsey/composer-install@v2
with:
composer-options: --optimize-autoloader
- run: composer psalm -- --php-version=${{ matrix.php }} --stats --output-format=github --shepherd

test:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1, 8.2]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
- uses: ramsey/composer-install@v2
with:
composer-options: --optimize-autoloader
- run: composer test -- --colors=always --order-by=random --coverage-clover coverage.xml
- if: ${{ matrix.php == '8.1' }}
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

infection:
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.1]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: pcov
- uses: ramsey/composer-install@v2
with:
composer-options: --optimize-autoloader
- run: composer infection
env:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/var/
/vendor/
/.php-cs-fixer.php
/infection.json5
/phpunit.xml
/psalm.xml
25 changes: 25 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use PHPyh\CodingStandard\PhpCsFixerCodingStandard;

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->append([
__FILE__,
])
->notPath([
'benchmark.php',
]);

$config = (new PhpCsFixer\Config())
->setFinder($finder)
->setCacheFile(__DIR__ . '/var/.php-cs-fixer.cache');

(new PhpCsFixerCodingStandard())->applyTo($config);

return $config;
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-present Valentin Udaltsov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Typhoon Overloard

The missing method overloading feature for PHP.

[![Latest Stable Version](https://poser.pugx.org/typhoon/overloading/v/stable.png)](https://packagist.org/packages/typhoon/overloading)
[![Total Downloads](https://poser.pugx.org/typhoon/overloading/downloads.png)](https://packagist.org/packages/typhoon/overloading)
[![psalm-level](https://shepherd.dev/github/typhoon-php/overloading/level.svg)](https://shepherd.dev/github/typhoon-php/overloading)
[![type-coverage](https://shepherd.dev/github/typhoon-php/overloading/coverage.svg)](https://shepherd.dev/github/typhoon-php/overloading)
[![Code Coverage](https://codecov.io/gh/typhoon-php/overloading/branch/0.1.x/graph/badge.svg)](https://codecov.io/gh/typhoon-php/overloading/tree/0.1.x)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Ftyphoon-php%2Foverloading%2F0.1.x)](https://dashboard.stryker-mutator.io/reports/github.com/typhoon-php/overloading/0.1.x)

## Installation

`composer require typhoon/overloading`

## Usage

To mark methods `handleInt` and `handleString` as overloading method `handle`,
add `#[Overload('handle')]` attribute to `handleInt` and `handleString` and
call `Overload::call()` from `handle`. You do not need to pass arguments to `Overload::call()`,
this happens automagically. However, return `Overload::call()` explicitly if you need to.
After this you will be able to call `handle` with any arguments and reach overloading methods
when their signature matches.

```php
final class WhateverHandler
{
public function handle(mixed ...$args): string
{
return Overload::call();
}

#[Overload('handle')]
public function handleInt(int $int): string
{
return __METHOD__;
}

#[Overload('handle')]
public function handleString(string $string): string
{
return __METHOD__;
}

#[Overload('handle')]
public function handleStdClass(\stdClass $object): string
{
return __METHOD__;
}

#[Overload('handle')]
public function handleNamedOptionalArguments(int $int = 0, float $float = M_E): string
{
return __METHOD__;
}
}

$handler = new WhateverHandler();

// WhateverHandler::handleInt
var_dump($handler->handle(300));

// WhateverHandler::handleString
var_dump($handler->handle('Hello world!'));

// WhateverHandler::handleStdClass
var_dump($handler->handle(new \stdClass()));

// WhateverHandler::handleNamedOptionalArguments
var_dump($handler->handle(float: 1.5));

// WhateverHandler::handleNamedOptionalArguments
var_dump($handler->handle());

// No matching overloading methods for WhateverHandler::handle(string, bool).
var_dump($handler->handle('Hey!', true));
```

## What about speed?

Well, using overloading is obviously slower, than calling a method directly, but not awfully slower.
Here's a simple benchmark for the `WhateverHandler`:

```php
// warm up
$handler->handle();

\DragonCode\Benchmark\Benchmark::start()
->withoutData()
->round(2)
->compare([
'direct call' => static fn (): string => $handler->handleNamedOptionalArguments(),
'overloaded call' => static fn (): string => $handler->handle(),
]);
```

```shell
------- ---------------- -------------------
# direct call overloaded call
------- ---------------- -------------------
min 0 ms - 0 bytes 0 ms - 0 bytes
max 0 ms - 0 bytes 0.02 ms - 0 bytes
avg 0 ms - 0 bytes 0 ms - 0 bytes
total 0.95 ms 1.16 ms
------- ---------------- -------------------
Order - 1 - - 2 -
------- ---------------- -------------------
```

It's important to understand that memoization plays a very important role here. CLI workers and applications, served
via Roadrunner, for instance, will benefit from this. For PHP-FPM you can enable file cache suitable for OPcaching via
`Overload::useFileCache('/path/to/cache');`.

## TODO

- [ ] Finish tests.
- [ ] Explain caching in README.
- [ ] Optimize generated code.
- [ ] Inherit attributes from upstream method declarations.
- [ ] Allow to warm up classes.
- [ ] Psalm plugin.
- [ ] PHPStan plugin.
- [ ] Support static analysis types.
6 changes: 6 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"symbol-whitelist": [
"opcache_compile_file",
"opcache_invalidate"
]
}

0 comments on commit b2a7ad3

Please sign in to comment.