Skip to content

Commit dfe2f29

Browse files
committed
Merge branch 'dev'
2 parents d49a104 + f614e59 commit dfe2f29

13 files changed

Lines changed: 535 additions & 4 deletions

File tree

.github/workflows/static.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Static Analysis
2+
on: [push, pull_request]
3+
jobs:
4+
phpstan:
5+
name: PHPStan
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v2
9+
- name: PHPStan
10+
uses: docker://oskarstark/phpstan-ga
11+
with:
12+
args: analyse src/

.github/workflows/tests.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Tests
2+
3+
on: ['push', 'pull_request']
4+
5+
jobs:
6+
build:
7+
name: PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
8+
runs-on: ${{ matrix.os }}
9+
strategy:
10+
matrix:
11+
os: [ubuntu-latest, macos-latest, windows-latest]
12+
php: ['7.3', '7.4', '8.0']
13+
dependency-version: [prefer-lowest, prefer-stable]
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- name: Setup PHP
19+
uses: shivammathur/setup-php@v2
20+
with:
21+
php-version: ${{ matrix.php }}
22+
tools: composer:v2
23+
extensions: json, mbstring
24+
coverage: xdebug
25+
26+
- name: Setup Problem Matches
27+
run: |
28+
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
29+
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
30+
31+
- name: Install PHP 7 dependencies
32+
run: composer update --dev --${{ matrix.dependency-version }} --no-interaction --no-progress
33+
if: "matrix.php < 8"
34+
35+
- name: Install PHP 8 dependencies
36+
run: composer update --dev --${{ matrix.dependency-version }} --ignore-platform-req=php --no-interaction --no-progress
37+
if: "matrix.php >= 8"
38+
39+
- name: Run Tests with Coverage
40+
run: ./vendor/bin/pest --coverage

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Global
2+
.composer
3+
composer.lock
4+
package-lock.json
5+
vendor/
6+
7+
# OS Generated
8+
.DS_Store*
9+
ehthumbs.db
10+
Icon?
11+
Thumbs.db
12+
*.swp
13+
14+
# phpstorm
15+
.idea/*

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<a name="1.0.0"></a>
2+
# [1.0.0](https://github.com/atomastic/macroable) (2020-12-05)
3+
* Initial release

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
MIT License
1+
The MIT License (MIT)
22

3-
Copyright (c) 2020 ATOMASTIC
3+
Copyright (c) 2020-2021 Sergey Romanenko
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 167 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,167 @@
1-
# macroable
2-
A trait to dynamically add methods to a class.
1+
<h1 align="center">Macroable Component</h1>
2+
<p align="center">
3+
Macroable Component is a trait that, gives you the ability in effect to add new methods to a class at runtime.
4+
</p>
5+
<p align="center">
6+
<a href="https://github.com/atomastic/macroable/releases"><img alt="Version" src="https://img.shields.io/github/release/atomastic/macroable.svg?label=version&color=green"></a> <a href="https://github.com/atomastic/macroable"><img src="https://img.shields.io/badge/license-MIT-blue.svg?color=green" alt="License"></a> <a href="https://packagist.org/packages/atomastic/macroable"><img src="https://poser.pugx.org/atomastic/macroable/downloads" alt="Total downloads"></a> <img src="https://github.com/atomastic/macroable/workflows/Static%20Analysis/badge.svg?branch=dev"> <img src="https://github.com/atomastic/macroable/workflows/Tests/badge.svg">
7+
<a href="https://app.codacy.com/gh/atomastic/macroable?utm_source=github.com&utm_medium=referral&utm_content=atomastic/macroable&utm_campaign=Badge_Grade_Dashboard"><img src="https://api.codacy.com/project/badge/Grade/72b4dc84c20145e1b77dc0004a3c8e3d"></a> <a href="https://codeclimate.com/github/atomastic/macroable/maintainability"><img src="https://api.codeclimate.com/v1/badges/a4c673a4640a3863a9a4/maintainability" /></a>
8+
</p>
9+
10+
<br>
11+
12+
* [Installation](#installation)
13+
* [Usage](#usage)
14+
* [Methods](#methods)
15+
* [Tests](#tests)
16+
* [License](#license)
17+
18+
### Installation
19+
20+
#### With [Composer](https://getcomposer.org)
21+
22+
```
23+
composer require atomastic/macroable
24+
```
25+
26+
### Usage
27+
28+
```php
29+
use Atomastic\Macroable\Macroable;
30+
```
31+
32+
### Methods
33+
34+
| Method | Description |
35+
|---|---|
36+
| <a href="#macroable_macro">`macro()`</a> | Register a custom macro. |
37+
| <a href="#macroable_mixin">`mixin()`</a> | Mix another object into the class. |
38+
| <a href="#macroable_hasMacro">`hasMacro()`</a> | Checks if macro is registered. |
39+
40+
#### Methods Details
41+
42+
##### <a name="macroable_macro"></a> Method: `macro()`
43+
44+
```php
45+
/**
46+
* Register a custom macro.
47+
*
48+
* @param string $name Name.
49+
* @param object|callable $macro Macro.
50+
* @return void
51+
*/
52+
public static function macro(string $name, $macro): void
53+
```
54+
55+
##### Example
56+
57+
```php
58+
$macroableClass = new class() {
59+
use Macroable;
60+
};
61+
62+
$macroableClass::macro('concatenate', function(... $strings) {
63+
return implode('-', $strings);
64+
});
65+
66+
$macroableClass::macro('message', function($name) {
67+
return 'Hello ' . $name;
68+
});
69+
70+
echo $macroableClass->concatenate('one', 'two', 'three');
71+
echo $macroableClass->message('Jack');
72+
```
73+
74+
##### The above example will output:
75+
76+
```
77+
one-two-three
78+
Hello Jack
79+
```
80+
81+
##### <a name="macroable_mixin"></a> Method: `mixin()`
82+
83+
```php
84+
/**
85+
* Mix another object into the class.
86+
*
87+
* @param object $mixin Mixin.
88+
* @param bool $replace Replace.
89+
* @return void
90+
*
91+
* @throws ReflectionException
92+
*/
93+
public static function mixin($mixin, bool $replace = true): void
94+
```
95+
96+
##### Example
97+
98+
```php
99+
$mixinClass = new class() {
100+
public function mixinMethod()
101+
{
102+
return function() {
103+
return 'mixinMethod';
104+
};
105+
}
106+
107+
public function anotherMixinMethod()
108+
{
109+
return function() {
110+
return 'anotherMixinMethod';
111+
};
112+
}
113+
};
114+
115+
$macroableClass->mixin($mixin);
116+
117+
$macroableClass->mixinMethod();
118+
$macroableClass->anotherMixinMethod();
119+
```
120+
121+
##### The above example will output:
122+
123+
```
124+
mixinMethod
125+
anotherMixinMethod
126+
```
127+
128+
129+
##### <a name="macroable_hasMacro"></a> Method: `hasMacro()`
130+
131+
```php
132+
/**
133+
* Checks if macro is registered.
134+
*
135+
* @param string $name Name
136+
* @return bool
137+
*/
138+
public static function hasMacro(string $name): bool
139+
```
140+
141+
##### Example
142+
143+
```php
144+
$macroableClass = new class() {
145+
use Macroable;
146+
};
147+
148+
$macroableClass::macro('message', function($name) {
149+
return 'Hello ' . $name;
150+
});
151+
152+
if ($macroableClass::hasMacro('message')) {
153+
// do something...
154+
}
155+
```
156+
157+
### Tests
158+
159+
Run tests
160+
161+
```
162+
./vendor/bin/pest
163+
```
164+
165+
### License
166+
[The MIT License (MIT)](https://github.com/atomastic/macroable/blob/master/LICENSE)
167+
Copyright (c) 2020 [Sergey Romanenko](https://github.com/Awilum)

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "atomastic/macroable",
3+
"description": "Macroable Component is a trait that, gives you the ability in effect to add new methods to a class at runtime.",
4+
"license": "MIT",
5+
"keywords": [
6+
"Macroable", "macroable", "php", "atomastic"
7+
],
8+
"support": {
9+
"source": "https://github.com/atomastic/macroable",
10+
"issues": "https://github.com/atomastic/macroable/issues"
11+
},
12+
"authors": [
13+
{
14+
"name": "Sergey Romanenko",
15+
"email": "sergey.romanenko@flextype.org",
16+
"homepage": "https://github.com/Awilum"
17+
}
18+
],
19+
"require": {
20+
"php": "^7.3 || ^8.0"
21+
},
22+
"autoload":{
23+
"psr-4": {
24+
"Atomastic\\Macroable\\": "src/"
25+
}
26+
},
27+
"require-dev": {
28+
"doctrine/coding-standard": "8.1.0",
29+
"pestphp/pest": "^0.3.3",
30+
"phpstan/phpstan": "^0.12.42"
31+
}
32+
}

phpstan.neon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- %currentWorkingDirectory%/src/
5+
- %currentWorkingDirectory%/tests/
6+
reportUnmatchedIgnoredErrors: false
7+
checkGenericClassInNonGenericObjectType: false
8+
checkMissingIterableValueType: false
9+
ignoreErrors:
10+
# ignore
11+
- '#Unsafe usage of new static\(\)\.#'

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<filter>
13+
<whitelist processUncoveredFilesFromWhitelist="true">
14+
<directory suffix=".php">./app</directory>
15+
<directory suffix=".php">./src</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

0 commit comments

Comments
 (0)