Skip to content

Commit 15b2d41

Browse files
authored
Add phpcs workflow (#8)
1 parent 8acc9ca commit 15b2d41

File tree

11 files changed

+129
-35
lines changed

11 files changed

+129
-35
lines changed

.github/workflows/cron.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Check Package
1+
name: packagist
22

33
on:
44
schedule:

.github/workflows/php-cs-fixer.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: stylefix
2+
3+
on: [push]
4+
5+
jobs:
6+
php-cs-fixer:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout code
11+
uses: actions/checkout@v3
12+
with:
13+
ref: ${{ github.head_ref }}
14+
15+
- name: Run PHP CS Fixer
16+
uses: docker://oskarstark/php-cs-fixer-ga
17+
with:
18+
args: --config=.php_cs.dist.php --allow-risky=yes
19+
20+
- name: Commit changes
21+
uses: stefanzweifel/git-auto-commit-action@v4
22+
with:
23+
commit_message: Fix styling

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Tests
1+
name: tests
22

33
on: [push, pull_request]
44

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: update-changelog
2+
3+
on:
4+
release:
5+
types: [released]
6+
7+
jobs:
8+
update:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v3
14+
with:
15+
ref: main
16+
17+
- name: Update Changelog
18+
uses: stefanzweifel/changelog-updater-action@v1
19+
with:
20+
latest-version: ${{ github.event.release.name }}
21+
release-notes: ${{ github.event.release.body }}
22+
23+
- name: Commit updated CHANGELOG
24+
uses: stefanzweifel/git-auto-commit-action@v4
25+
with:
26+
branch: main
27+
commit_message: Update CHANGELOG
28+
file_pattern: CHANGELOG.md

.php_cs.dist.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
$finder = Symfony\Component\Finder\Finder::create()
4+
->in([
5+
__DIR__ . '/src',
6+
__DIR__ . '/tests',
7+
])
8+
->name('*.php')
9+
->notName('*.blade.php')
10+
->ignoreDotFiles(true)
11+
->ignoreVCS(true);
12+
13+
return (new PhpCsFixer\Config())
14+
->setRules([
15+
'@PSR12' => true,
16+
'array_syntax' => ['syntax' => 'short'],
17+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
18+
'no_unused_imports' => true,
19+
'not_operator_with_successor_space' => true,
20+
'trailing_comma_in_multiline' => true,
21+
'phpdoc_scalar' => true,
22+
'unary_operator_spaces' => true,
23+
'binary_operator_spaces' => true,
24+
'blank_line_before_statement' => [
25+
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
26+
],
27+
'phpdoc_single_line_var_spacing' => true,
28+
'phpdoc_var_without_name' => true,
29+
'class_attributes_separation' => [
30+
'elements' => [
31+
'method' => 'one',
32+
],
33+
],
34+
'method_argument_space' => [
35+
'on_multiline' => 'ensure_fully_multiline',
36+
'keep_multiple_spaces_after_comma' => true,
37+
],
38+
'single_trait_insert_per_statement' => true,
39+
])
40+
->setFinder($finder);

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# OpenCF
22

3-
![Tests](https://github.com/phpjuice/opencf/workflows/Tests/badge.svg?branch=main)
4-
[![Check Package](https://github.com/phpjuice/opencf/actions/workflows/cron.yml/badge.svg)](https://github.com/phpjuice/opencf/actions/workflows/cron.yml)
3+
[![tests](https://github.com/phpjuice/opencf/actions/workflows/php.yml/badge.svg?branch=main)](https://github.com/phpjuice/opencf/actions/workflows/php.yml)
4+
[![packagist](https://github.com/phpjuice/opencf/actions/workflows/cron.yml/badge.svg?branch=main)](https://github.com/phpjuice/opencf/actions/workflows/cron.yml)
55
[![Maintainability](https://api.codeclimate.com/v1/badges/60b1fac54adddd5a4e12/maintainability)](https://codeclimate.com/github/phpjuice/opencf/maintainability)
66
[![Latest Stable Version](http://poser.pugx.org/phpjuice/opencf/v)](https://packagist.org/packages/phpjuice/opencf)
77
[![Total Downloads](http://poser.pugx.org/phpjuice/opencf/downloads)](https://packagist.org/packages/phpjuice/opencf)
@@ -144,8 +144,8 @@ the [tags on this repository](https://github.com/PHPJuice/opencf/tags).
144144

145145
license. Please see the [Licence](https://github.com/phpjuice/opencf/blob/main/LICENSE) for more information.
146146

147-
![Tests](https://github.com/phpjuice/opencf/workflows/Tests/badge.svg?branch=main)
148-
[![Check Package](https://github.com/phpjuice/opencf/actions/workflows/cron.yml/badge.svg)](https://github.com/phpjuice/opencf/actions/workflows/cron.yml)
147+
[![tests](https://github.com/phpjuice/opencf/actions/workflows/php.yml/badge.svg?branch=main)](https://github.com/phpjuice/opencf/actions/workflows/php.yml)
148+
[![packagist](https://github.com/phpjuice/opencf/actions/workflows/cron.yml/badge.svg?branch=main)](https://github.com/phpjuice/opencf/actions/workflows/cron.yml)
149149
[![Maintainability](https://api.codeclimate.com/v1/badges/60b1fac54adddd5a4e12/maintainability)](https://codeclimate.com/github/phpjuice/opencf/maintainability)
150150
[![Latest Stable Version](http://poser.pugx.org/phpjuice/opencf/v)](https://packagist.org/packages/phpjuice/opencf)
151151
[![Total Downloads](http://poser.pugx.org/phpjuice/opencf/downloads)](https://packagist.org/packages/phpjuice/opencf)

docs/README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# OpenCF
22

3-
![Tests](https://github.com/phpjuice/opencf/workflows/Tests/badge.svg?branch=main)
4-
[![Check Package](https://github.com/phpjuice/opencf/actions/workflows/cron.yml/badge.svg)](https://github.com/phpjuice/opencf/actions/workflows/cron.yml)
3+
[![tests](https://github.com/phpjuice/opencf/actions/workflows/php.yml/badge.svg?branch=main)](https://github.com/phpjuice/opencf/actions/workflows/php.yml)
4+
[![packagist](https://github.com/phpjuice/opencf/actions/workflows/cron.yml/badge.svg?branch=main)](https://github.com/phpjuice/opencf/actions/workflows/cron.yml)
55
[![Maintainability](https://api.codeclimate.com/v1/badges/60b1fac54adddd5a4e12/maintainability)](https://codeclimate.com/github/phpjuice/opencf/maintainability)
66
[![Latest Stable Version](http://poser.pugx.org/phpjuice/opencf/v)](https://packagist.org/packages/phpjuice/opencf)
77
[![Total Downloads](http://poser.pugx.org/phpjuice/opencf/downloads)](https://packagist.org/packages/phpjuice/opencf)
@@ -69,10 +69,3 @@ This should produce the following results:
6969
"nautilus" => 0.1
7070
];
7171
```
72-
73-
![Tests](https://github.com/phpjuice/opencf/workflows/Tests/badge.svg?branch=main)
74-
[![Check Package](https://github.com/phpjuice/opencf/actions/workflows/cron.yml/badge.svg)](https://github.com/phpjuice/opencf/actions/workflows/cron.yml)
75-
[![Maintainability](https://api.codeclimate.com/v1/badges/60b1fac54adddd5a4e12/maintainability)](https://codeclimate.com/github/phpjuice/opencf/maintainability)
76-
[![Latest Stable Version](http://poser.pugx.org/phpjuice/opencf/v)](https://packagist.org/packages/phpjuice/opencf)
77-
[![Total Downloads](http://poser.pugx.org/phpjuice/opencf/downloads)](https://packagist.org/packages/phpjuice/opencf)
78-
[![License](http://poser.pugx.org/phpjuice/opencf/license)](https://packagist.org/packages/phpjuice/opencf)

src/Algorithms/Recommender.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function buildModel(): self
6767
$this->dataset[$k1],
6868
$this->dataset[$k2]
6969
);
70+
7071
try {
7172
// save the measure to the model
7273
$this->model[$k1][$k2] =
@@ -95,6 +96,7 @@ public function predict(array $userRatings): array
9596
if (isset($userRatings[$key])) {
9697
continue;
9798
}
99+
98100
try {
99101
$predictions[$key] = $this->predictor
100102
->getPrediction($userRatings, $key);

src/RecommenderService.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function setDataset(array $dataset = []): self
7373

7474
public function weightedSlopeone(): IRecommender
7575
{
76-
if (!in_array(WeightedSlopeone::class, $this->recommenders)) {
76+
if (! in_array(WeightedSlopeone::class, $this->recommenders)) {
7777
$this->registerRecommender(WeightedSlopeone::class);
7878
}
7979

@@ -85,16 +85,22 @@ public function registerRecommender(string $recommender): self
8585
try {
8686
$rf = new ReflectionClass($recommender);
8787
} catch (ReflectionException $e) {
88-
throw new NotSupportedSchemeException(sprintf('Recommendation engine "%s" must implement "%s" interface',
89-
$recommender, IRecommender::class));
88+
throw new NotSupportedSchemeException(sprintf(
89+
'Recommendation engine "%s" must implement "%s" interface',
90+
$recommender,
91+
IRecommender::class
92+
));
9093
}
9194

92-
if (!$rf->implementsInterface(IRecommender::class)) {
93-
throw new NotSupportedSchemeException(sprintf('Recommendation engine "%s" must implement "%s" interface',
94-
$recommender, IRecommender::class));
95+
if (! $rf->implementsInterface(IRecommender::class)) {
96+
throw new NotSupportedSchemeException(sprintf(
97+
'Recommendation engine "%s" must implement "%s" interface',
98+
$recommender,
99+
IRecommender::class
100+
));
95101
}
96102

97-
if (!in_array($recommender, $this->recommenders)) {
103+
if (! in_array($recommender, $this->recommenders)) {
98104
$this->recommenders[$recommender] = new $recommender($this->dataset);
99105
}
100106

@@ -103,17 +109,19 @@ public function registerRecommender(string $recommender): self
103109

104110
public function getRecommender(string $recommender): IRecommender
105111
{
106-
if (!array_key_exists($recommender, $this->recommenders)) {
107-
throw new NotRegisteredRecommenderException(sprintf('The Recommendation engine "%s" is not registered in the Recommender Service',
108-
$recommender));
112+
if (! array_key_exists($recommender, $this->recommenders)) {
113+
throw new NotRegisteredRecommenderException(sprintf(
114+
'The Recommendation engine "%s" is not registered in the Recommender Service',
115+
$recommender
116+
));
109117
}
110118

111119
return $this->recommenders[$recommender]->buildModel();
112120
}
113121

114122
public function weightedCosine(): IRecommender
115123
{
116-
if (!in_array(WeightedCosine::class, $this->recommenders)) {
124+
if (! in_array(WeightedCosine::class, $this->recommenders)) {
117125
$this->registerRecommender(WeightedCosine::class);
118126
}
119127

@@ -122,7 +130,7 @@ public function weightedCosine(): IRecommender
122130

123131
public function cosine(): IRecommender
124132
{
125-
if (!in_array(Cosine::class, $this->recommenders)) {
133+
if (! in_array(Cosine::class, $this->recommenders)) {
126134
$this->registerRecommender(Cosine::class);
127135
}
128136

tests/IntegrationTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
"user1" => [
88
"squid" => 1,
99
"cuttlefish" => 0.5,
10-
"octopus" => 0.2
10+
"octopus" => 0.2,
1111
],
1212
"user2" => [
1313
"squid" => 1,
1414
"octopus" => 0.5,
15-
"nautilus" => 0.2
15+
"nautilus" => 0.2,
1616
],
1717
"user3" => [
1818
"squid" => 0.2,
1919
"octopus" => 1,
2020
"cuttlefish" => 0.4,
21-
"nautilus" => 0.4
21+
"nautilus" => 0.4,
2222
],
2323
"user4" => [
2424
"cuttlefish" => 0.9,
2525
"octopus" => 0.4,
26-
"nautilus" => 0.5
27-
]
26+
"nautilus" => 0.5,
27+
],
2828
]);
2929

3030
// Create an instance
@@ -35,12 +35,12 @@
3535

3636
// Predict future ratings
3737
$results = $recommender->predict([
38-
'squid' => 0.4
38+
'squid' => 0.4,
3939
]);
4040

4141
expect($results)->toBe([
4242
"cuttlefish" => 0.25,
4343
"octopus" => 0.23,
44-
"nautilus" => 0.1
44+
"nautilus" => 0.1,
4545
]);
4646
});

0 commit comments

Comments
 (0)