Skip to content

Commit b855165

Browse files
authored
Introduce Sonarcloud (#14)
* Introduce Sonarcloud * markdown * No mdlint * Add feth depth * Phpstan export * Step names
1 parent 76b752a commit b855165

File tree

5 files changed

+96
-17
lines changed

5 files changed

+96
-17
lines changed

.github/workflows/ci.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: CI
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [ main ]
7+
tags: [ v* ]
8+
pull_request:
9+
10+
env:
11+
PHP_VERSION: 8.2
12+
13+
jobs:
14+
validate:
15+
name: 'Build & validate'
16+
runs-on: ubuntu-22.04
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
- name: Install PHP
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: ${{ env.PHP_VERSION }}
26+
extensions: xdebug, zip, pcov
27+
env:
28+
GITHUB_TOKEN: ${{ secrets.REPO_READONLY_TOKEN }}
29+
- name: 'Composer install'
30+
env:
31+
COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.REPO_READ_ONLY_TOKEN }}"}}'
32+
run: composer install --no-interaction --no-scripts --no-progress --prefer-dist --no-ansi
33+
- name: Static analysis with PHPStan
34+
run: composer phpstan
35+
- name: Coding style PSR12 Check
36+
run: composer phpcs
37+
- name: Execute tests (Unit and Feature)
38+
run: composer test
39+
- name: Run SonarCloud scanner
40+
uses: minvws/nl-irealisatie-generic-pipelines/.github/actions/sonarcloud@main
41+
with:
42+
sonar-token: ${{ secrets.SONAR_TOKEN }}

.markdownlint-cli2.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
config:
2+
line-length:
3+
strict: false
4+
code_blocks: false
5+
line_length: 256
6+
7+
globs:
8+
- '**/*.md'
9+
10+
gitignore: true
11+
showFound: true

README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Codable (minvws/codable)
22

3-
Codable allows you to convert types into and out of an external representation (for example JSON).
3+
Codable allows you to convert types into and out of an external representation (for example JSON).
44

5-
It is inspired by Swift's [Encoding/Decoding/Serialization](https://developer.apple.com/documentation/swift/encoding-decoding-and-serialization)
5+
It is inspired by Swift's [Encoding/Decoding/Serialization](https://developer.apple.com/documentation/swift/encoding-decoding-and-serialization)
66
library, but includes some unique features like delegates.
77

88
## Features
@@ -20,6 +20,7 @@ library, but includes some unique features like delegates.
2020
- Composer
2121

2222
## Installation
23+
2324
Install the package through composer. Since this is currently a private package, you must
2425
enable the repository in your `composer.json` file:
2526

@@ -40,7 +41,8 @@ After that, you can install the package:
4041
composer require minvws/codable
4142
```
4243

43-
## Usage
44+
## Usage
45+
4446
### Decoding
4547

4648
There are several ways in which you can use Codable to decode, for example, a JSON snippet:
@@ -102,7 +104,6 @@ echo "$firstName doesn't like " . implode(',', $dislikedVegetables) . "\n";
102104
Although we need a few more method calls the code now automatically throws an exception if the structure of, or types
103105
used in, the JSON is not as we expected.
104106

105-
106107
#### Decoding using property attributes
107108

108109
To make our life a little easier, and use auto-completion in our IDE, we can decode the JSON in our own types. Let's
@@ -158,14 +159,14 @@ As you can see our classes implement the `Decodable` interface. This lets Codabl
158159
object yourself. We use the `DecodableSupport` trait so that we don't have to write the decoding code ourselves.
159160
Codable uses reflection to determine field names, types etc. It also checks if it needs to inject values using the
160161
constructor or if it can simply assign the values to object properties (even `private` and `protected` properties
161-
are supported).
162+
are supported).
162163

163164
Unfortunately PHP doesn't let you statically type arrays, but by using the `CodableArray` attribute we can let
164165
Codable know what types to expect for the array's elements.
165166

166167
The `CodableName` attribute allows us to use a different name for our class property than what is used in the JSON. We
167-
set an expected date/time format for the birthdate using the `CodableDateTime` attribute, although Codable is
168-
just as happy to simply let PHP's DateTime classes determine if they can parse a given date. We can also make fields
168+
set an expected date/time format for the birthdate using the `CodableDateTime` attribute, although Codable is
169+
just as happy to simply let PHP's DateTime classes determine if they can parse a given date. We can also make fields
169170
optional, in which case a null value will be assigned if the field is missing or contains a null value in the JSON.
170171

171172
Backed enumerations are decoded using their backed value. Enumerations that are not backed by an integer or string value
@@ -227,7 +228,7 @@ to not exist in the JSON, but if it does exist it needs to contain a non-null va
227228

228229
Sometimes your code needs to interface with a library you didn't write yourself and contains types you want to decode
229230
into or sometimes you want decode different pieces of JSON to the same type. To make this possible you can choose to
230-
write a delegate class. Your delegate class can either implement the `DecodableDelegate` or
231+
write a delegate class. Your delegate class can either implement the `DecodableDelegate` or
231232
`StaticDecodableDelegate` interface with either a non-static or static `decode` method. Let's look at an example:
232233

233234
```php
@@ -254,13 +255,13 @@ $container = $decoder->decode($json);
254255
$person = $container->decode(Person::class);
255256
```
256257

257-
This even works if your class has its own `Decodable` implementation and also works multiple levels deep in the
258-
decoding hierarchy.
258+
This even works if your class has its own `Decodable` implementation and also works multiple levels deep in the decoding hierarchy.
259259

260260
To register a `StaticDecodableDelegate` you can simply register its class. You can even register a `callable` as a
261261
delegate in which case it will receive the `DecodingContainer` and optional existing instance as its arguments.
262262

263263
### Encoding
264+
264265
Codable also supports encoding of your custom types to JSON (or other serialization formats). There are several
265266
ways to implement this:
266267

@@ -279,6 +280,7 @@ $person = new Person(...);
279280
$encoder = new JSONEncoder();
280281
echo $encoder->encode($person);
281282
```
283+
282284
This works similar to how PHP's `json_encode` would encode your types, with the most notable exception that
283285
DateTime objects will be encoded to an ISO-8601 date/time string. This also means for your objects that only public
284286
properties will be encoded.
@@ -315,6 +317,7 @@ readonly class Person implements Codable
315317
// ...
316318
}
317319
```
320+
318321
You can use the same attributes as mentioned earlier, but as Codable also has access to your `private` and
319322
`protected` properties there is an additional attribute that might come in handy; `CodableIgnore`. This attribute lets
320323
you control wetter a property should be ignored when encoding, decoding or both.
@@ -360,8 +363,9 @@ final readonly class Person implements Encodable
360363
}
361364
}
362365
```
366+
363367
This way you can even choose to encode nested objects inside the owner class instead of delegating it to the
364-
respective class.
368+
respective class.
365369

366370
If you assign values to the container Codable will automatically try to determine the best way to encode the value.
367371
But you can also choose to explicitly encode to a certain type using one of the `encode<type>` methods.
@@ -394,6 +398,7 @@ $encoder = new JSONEncoder();
394398
$encoder->getContext()->registerDelegate(Person::class, new PersonEncodableDelegate());
395399
$json = $encoder->encode($person);
396400
```
401+
397402
This even works if your class has its own `Encodable` implementation and also works multiple levels deep in the
398403
encoding hierarchy.
399404

@@ -410,10 +415,9 @@ request on the GitHub repository of this package.
410415

411416
## License
412417

413-
This package is open-source and released under the
414-
[European Union Public License version 1.2](LICENSE.txt).
418+
This package is open-source and released under the [European Union Public License version 1.2](LICENSE.txt).
415419
You are free to use, modify, and distribute the package in accordance with the terms of the license.
416420

417-
418421
## Part of iCore
419-
This package is part of the iCore project.
422+
423+
This package is part of the iCore project.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
}
3030
},
3131
"scripts": {
32-
"phpstan": "vendor/bin/phpstan analyze -c phpstan.neon.dist",
33-
"phpcs": "vendor/bin/phpcs"
32+
"phpstan": "vendor/bin/phpstan analyze -c phpstan.neon.dist --error-format=prettyJson > phpstan.json",
33+
"phpcs": "vendor/bin/phpcs",
34+
"test": "XDEBUG_MODE=coverage phpunit --log-junit=report-phpunit.xml --coverage-text --coverage-clover=coverage-phpunit.xml"
3435
}
3536
}

sonar-project.properties

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Project info
2+
sonar.organization=vws
3+
sonar.projectKey=nl-rdo-php-codable
4+
sonar.projectName=nl-rdo-php-codable
5+
6+
# Info links shown in SonarCloud
7+
sonar.links.homepage=https://github.com/minvws/nl-rdo-php-codable
8+
9+
# Encoding and language specific properties
10+
sonar.sourceEncoding=UTF-8
11+
12+
# Analysis scoping
13+
sonar.sources=src
14+
sonar.tests=tests
15+
16+
# Code coverage
17+
sonar.php.tests.reportPath=report-phpunit.xml
18+
sonar.php.coverage.reportPaths=coverage-phpunit.xml
19+
20+
# Additional reports
21+
sonar.php.phpstan.reportPaths=phpstan.json

0 commit comments

Comments
 (0)