Skip to content

Commit

Permalink
Merge branch 'fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
gnugat committed Dec 13, 2024
2 parents 43c7c8e + 4053c97 commit 5778628
Show file tree
Hide file tree
Showing 24 changed files with 420 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/composer.phar
/composer.lock
/vendor/

# Temporary files
/.php-cs-fixer.cache
92 changes: 92 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('bin')
->exclude('cache')
->exclude('doc')
->exclude('logs')
->exclude('vendor')
;

return (new PhpCsFixer\Config())
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setRules([
'@Symfony' => true,

'array_indentation' => true,
'array_syntax' => [
'syntax' => 'short',
],
'trailing_comma_in_multiline' => [
'elements' => ['arrays', 'arguments', 'parameters'],
],
'dir_constant' => true,
'ereg_to_preg' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'fopen_flag_order' => true,
'single_line_comment_style' => [
'comment_types' => ['hash'],
],
'heredoc_indentation' => true,
'heredoc_to_nowdoc' => true,
'implode_call' => true,
'is_null' => true,
'linebreak_after_opening_tag' => true,
'list_syntax' => [
'syntax' => 'short',
],
'logical_operators' => true,
'method_chaining_indentation' => true,
'modernize_types_casting' => true,
'multiline_whitespace_before_semicolons' => true,
'no_alias_functions' => true,
'no_alternative_syntax' => true,
'no_null_property_initialization' => true,
'no_php4_constructor' => true,
'echo_tag_syntax' => [
'format' => 'long',
],
'no_superfluous_elseif' => true,
'no_unneeded_final_method' => true,
'no_unreachable_default_argument_value' => true,
'no_unset_cast' => true,
'no_unset_on_property' => true,
'no_useless_else' => true,
'ordered_class_elements' => true,
'ordered_interfaces' => true,
'php_unit_expectation' => true,
'php_unit_method_casing' => [
'case' => 'snake_case',
],
'php_unit_mock_short_will_return' => true,
'php_unit_namespaced' => true,
'php_unit_no_expectation_annotation' => true,
'php_unit_set_up_tear_down_visibility' => true,
'php_unit_strict' => true,
'php_unit_test_annotation' => [
'style' => 'annotation',
],
'php_unit_test_case_static_method_calls' => [
'call_type' => 'self',
],
'pow_to_exponentiation' => true,
'protected_to_private' => true,
'psr_autoloading' => true,
'self_accessor' => true,
'set_type_to_cast' => true,
'simple_to_complex_string_variable' => true,
'static_lambda' => true,
'strict_comparison' => true,
'strict_param' => true,
'ternary_to_null_coalescing' => true,
'visibility_required' => [
'elements' => ['property', 'const'],
],
'void_return' => true,
'declare_strict_types' => true,
])
->setUsingCache(true)
->setFinder($finder)
;
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 0.0.3: Tidied up project

* Introduced Coding Standards
* Added License headers
* Harmonised `composer.json` files

## 0.0.2: Created `scc/exception`

When an error occurs, one of the following exception has to be thrown:
Expand Down
97 changes: 97 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Here's some tips to make you the best contributor ever:
* [Tests](#tests)
* [Keeping your fork up-to-date](#keeping-your-fork-up-to-date)
* [Monorepo Maintenance](#monorepo-maintainance)
* [Add a new Package](#add-a-new-package)

## Tests

Expand Down Expand Up @@ -98,3 +99,99 @@ Alternatively use `major`, `minor`, `patch` instead of the version number:
```console
vendor/bin/monorepo-builder release patch # if last version was `v7.0.1`, will release `v7.0.2`
```

## Add a new Package

### Libraries

To add a new library, first pick a name for it (ideally short, maybe one word).

Example:

* name: `Exception` (snake case: `exception`)
* composer package name: `ssc/exception`
* PHP namespace: `Ssc\Exception`

Next, create a new folder in `./packages` with the following tree structure:

```
./packages/exception/
├── composer.json
├── LICENSE
├── README.md
├── spec/
└── src/
```

Then, add a section in the root `phpspec.yml`:

```yaml
suites:
exception:
src_path: packages/exception/src
spec_path: packages/exception
```
After that, add a new line in the root `README.md`:

```
[...]

## Libraries

* [exception]packages/exception/README.md)

[...]
```
Finally, require it in the root `composer.json`, using `*@dev` as a version:
```json
{
"require": {
"php": "^8.3",
"ssc/exception": "*@dev"
}
}
```

Don't forget to also add a line in the root `CHANGELOG.md`:

```
# CHANGELOG
## {{ new_version }}: Created `scc/exception`
When an error occurs, one of the following exception has to be thrown:
* **400** `ClientErrorException`: request with malformed syntax (example: invalid JSON)
* **401** `UnauthorizedException`: an authentication error (missing or invalid credentials)
* **403** `ForbiddenException`: an authorization error (credentials found and valid, but not allowed for this resource)
* **404** `NotFoundException`: an error when trying to locate a resource (doesn't exist or has been removed)
* **422** `ValidationFailedException`: an error when trying to validate a resource
* **500** `ServerErrorException`: application crashed
All of those exceptions are a subtype of `SscException` which provides
the possibility to add an error code:
```php
throw NotFoundException::make('No Product found for ID 42')
->withCode(4423)
;
```

[...]
```
Finally, add library to `.github/workflows/split_monorepo.yaml`:
```yaml
jobs:
split_monorepo:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
package:
- exception
```
39 changes: 36 additions & 3 deletions bin/test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
#!/usr/bin/env sh
#!/usr/bin/env bash

SHORT_HELP="$0 [-h] [-f]'"

while getopts ":hf" opt; do
case $opt in
h)
echo 'Usage:'
echo " $SHORT_HELP"
echo ''
echo 'Options:'
echo ' -f Fix coding style'
echo ''
exit 0
;;
f)
echo ''
echo '// Fixing CS...'
echo ''

vendor/bin/php-cs-fixer fix --allow-risky=yes
exit 0
;;
\?)
echo '' >&2
echo " [KO] The \"-$OPTARG\" option does not exist" >&2
echo '' >&2
echo " $SHORT_HELP" >&2
echo '' >&2
exit 1
;;
esac
done
unset SHORT_HELP

echo ''
echo '// Running tests...'

composer --quiet dump --optimize

vendor/bin/phpspec --no-interaction run -fdot #&& \
vendor/bin/phpspec --no-interaction run -fdot && \
# vendor/bin/phpunit && \
# PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run
vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{
"name": "ssc/lib",
"type": "project",
"description": "Decoupled PHP libraries",
"license": "MIT",
"type": "project",
"keywords": [
"ssc"
],
"authors": [
{
"name": "Loïc Faugeron",
"email": "[email protected]",
"homepage": "https://gnugat.github.io",
"role": "Developer"
"email": "[email protected]"
}
],
"homepage": "https://gnugat.github.io/ssc-lib",
Expand Down
2 changes: 1 addition & 1 deletion monorepo-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symplify\MonorepoBuilder\Release\ReleaseWorker\TagVersionReleaseWorker;

return static function (MBConfig $mbConfig): void {
$mbConfig->packageDirectories([__DIR__ . '/packages']);
$mbConfig->packageDirectories([__DIR__.'/packages']);

// for `release`
$mbConfig->workers([
Expand Down
Empty file modified packages/exception/README.md
100755 → 100644
Empty file.
17 changes: 17 additions & 0 deletions packages/exception/composer.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
"type": "library",
"description": "SSC - Exception",
"license": "MIT",
"keywords": [
"ssc"
],
"authors": [
{
"name": "Loïc Faugeron",
"email": "[email protected]"
}
],
"homepage": "https://gnugat.github.io/ssc-lib",
"require": {
"php": "^8.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.65",
"phpspec/phpspec": "^7.5"
},
"autoload": {
"psr-4": {
"Ssc\\Exception\\": "src"
}
},
"config": {
"sort-packages": true
}
}
6 changes: 6 additions & 0 deletions packages/exception/phpspec.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
suites:
exception:
namespace: Ssc\Exception
psr4_prefix: Ssc\Exception
src_path: src
spec_path: .
13 changes: 12 additions & 1 deletion packages/exception/spec/ClientErrorExceptionSpec.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
<?php

/*
* This file is part of the ssc/lib package.
*
* (c) Loïc Faugeron <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Ssc\Exception;

use Ssc\Exception\SscException;
use PhpSpec\ObjectBehavior;
use Ssc\Exception\SscException;

class ClientErrorExceptionSpec extends ObjectBehavior
{
Expand Down
13 changes: 12 additions & 1 deletion packages/exception/spec/ForbiddenExceptionSpec.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
<?php

/*
* This file is part of the ssc/lib package.
*
* (c) Loïc Faugeron <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Ssc\Exception;

use Ssc\Exception\SscException;
use PhpSpec\ObjectBehavior;
use Ssc\Exception\SscException;

class ForbiddenExceptionSpec extends ObjectBehavior
{
Expand Down
13 changes: 12 additions & 1 deletion packages/exception/spec/NotFoundExceptionSpec.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
<?php

/*
* This file is part of the ssc/lib package.
*
* (c) Loïc Faugeron <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Ssc\Exception;

use Ssc\Exception\SscException;
use PhpSpec\ObjectBehavior;
use Ssc\Exception\SscException;

class NotFoundExceptionSpec extends ObjectBehavior
{
Expand Down
Loading

0 comments on commit 5778628

Please sign in to comment.