Skip to content

Commit aa8cefd

Browse files
committed
chore: Add rector and cs-fix
Signed-off-by: Carl Schwan <[email protected]>
1 parent 137211d commit aa8cefd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+5895
-1032
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This workflow is provided via the organization template repository
2+
#
3+
# https://github.com/nextcloud/.github
4+
# https://docs.github.com/en/actions/learn-github-actions/sharing-workflows-with-your-organization
5+
#
6+
# SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors
7+
# SPDX-License-Identifier: MIT
8+
9+
name: Lint php-cs
10+
11+
on: pull_request
12+
13+
permissions:
14+
contents: read
15+
16+
concurrency:
17+
group: lint-php-cs-${{ github.head_ref || github.run_id }}
18+
cancel-in-progress: true
19+
20+
jobs:
21+
lint:
22+
runs-on: ubuntu-latest
23+
24+
name: php-cs
25+
26+
steps:
27+
- name: Checkout
28+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
29+
with:
30+
persist-credentials: false
31+
32+
- name: Get php version
33+
id: versions
34+
uses: icewind1991/nextcloud-version-matrix@58becf3b4bb6dc6cef677b15e2fd8e7d48c0908f # v1.3.1
35+
36+
- name: Set up php${{ steps.versions.outputs.php-min }}
37+
uses: shivammathur/setup-php@0f7f1d08e3e32076e51cae65eb0b0c871405b16e # v2.34.1
38+
with:
39+
php-version: ${{ steps.versions.outputs.php-min }}
40+
extensions: bz2, ctype, curl, dom, fileinfo, gd, iconv, intl, json, libxml, mbstring, openssl, pcntl, posix, session, simplexml, xmlreader, xmlwriter, zip, zlib, sqlite, pdo_sqlite
41+
coverage: none
42+
ini-file: development
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
46+
- name: Install dependencies
47+
run: |
48+
composer remove nextcloud/ocp --dev --no-scripts
49+
composer i
50+
51+
- name: Lint
52+
run: composer run cs:check || ( echo 'Please run `composer run cs:fix` to format your code' && exit 1 )

.php-cs-fixer.dist.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
require_once './vendor-bin/cs-fixer/vendor/autoload.php';
9+
10+
use Nextcloud\CodingStandard\Config;
11+
12+
$config = new Config();
13+
$config
14+
->getFinder()
15+
->ignoreVCSIgnored(true)
16+
->notPath('build')
17+
->notPath('tests/stubs')
18+
->notPath('l10n')
19+
->notPath('src')
20+
->notPath('vendor')
21+
->in(__DIR__);
22+
return $config;

appinfo/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
45
* SPDX-License-Identifier: AGPL-3.0-or-later

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
"scripts": {
2828
"bin": "echo 'bin not installed'",
2929
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
30+
"cs:check": "php-cs-fixer fix --dry-run --diff",
31+
"cs:fix": "php-cs-fixer fix",
3032
"openapi": "generate-spec --verbose",
33+
"rector": "rector && composer cs:fix",
3134
"post-install-cmd": [
3235
"@composer bin all install --ansi",
3336
"composer dump-autoload"

lib/AppInfo/Application.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
45
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -8,17 +9,16 @@
89

910
use Exception;
1011
use OC\Files\Filesystem;
11-
use OC\Files\Storage\Wrapper\Wrapper;
1212
use OCA\Registration\Events\PassedFormEvent;
1313
use OCA\Registration\Events\ShowFormEvent;
1414
use OCA\Registration\Events\ValidateFormEvent;
15-
use OCA\TermsOfService\PublicCapabilities;
1615
use OCA\TermsOfService\Checker;
1716
use OCA\TermsOfService\Dav\CheckPlugin;
1817
use OCA\TermsOfService\Filesystem\StorageWrapper;
1918
use OCA\TermsOfService\Listener\RegistrationIntegration;
2019
use OCA\TermsOfService\Listener\UserDeletedListener;
2120
use OCA\TermsOfService\Notifications\Notifier;
21+
use OCA\TermsOfService\PublicCapabilities;
2222
use OCP\AppFramework\App;
2323
use OCP\AppFramework\Bootstrap\IBootContext;
2424
use OCP\AppFramework\Bootstrap\IBootstrap;
@@ -34,8 +34,8 @@
3434
use OCP\User\Events\UserDeletedEvent;
3535
use OCP\User\Events\UserFirstTimeLoggedInEvent;
3636
use OCP\Util;
37-
use Psr\Log\LoggerInterface;
3837
use Psr\Container\ContainerExceptionInterface;
38+
use Psr\Log\LoggerInterface;
3939

4040
include_once __DIR__ . '/../../vendor/autoload.php';
4141

@@ -46,7 +46,7 @@ class Application extends App implements IBootstrap {
4646

4747

4848
public function __construct() {
49-
parent::__construct('terms_of_service');
49+
parent::__construct(self::APPNAME);
5050
}
5151

5252
public function register(IRegistrationContext $context): void {
@@ -61,7 +61,7 @@ public function boot(IBootContext $context): void {
6161
Util::connectHook('OC_Filesystem', 'preSetup', $this, 'addStorageWrapper');
6262

6363
$eventDispatcher = $context->getServerContainer()->get(IEventDispatcher::class);
64-
$eventDispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event) use ($context) {
64+
$eventDispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event) use ($context): void {
6565
$eventServer = $event->getServer();
6666

6767
if ($eventServer !== null) {
@@ -105,14 +105,14 @@ public function registerFrontend(IRequest $request, IConfig $config, IUserSessio
105105
// registration
106106
'#^/apps/registration(?:$|/)#',
107107
];
108-
if (array_filter($skipPatterns, fn($pattern) => preg_match($pattern, $request->getPathInfo()))) {
108+
if (array_filter($skipPatterns, fn (string $pattern): int|false => preg_match($pattern, $request->getPathInfo()))) {
109109
return;
110110
}
111111

112112
if ($userSession->getUser() instanceof IUser) {
113113
// Logged-in user
114114
Util::addScript('terms_of_service', 'terms_of_service-user');
115-
} else if ($config->getAppValue(self::APPNAME, 'tos_on_public_shares', '0') === '1') {
115+
} elseif ($config->getAppValue(self::APPNAME, 'tos_on_public_shares', '0') === '1') {
116116
// Guests on public pages
117117
Util::addScript('terms_of_service', 'terms_of_service-public');
118118
}
@@ -126,9 +126,6 @@ public function addStorageWrapper(): void {
126126
}
127127

128128
/**
129-
* @param string $mountPoint
130-
* @param IStorage $storage
131-
*
132129
* @return StorageWrapper|IStorage
133130
* @throws Exception
134131
*/
@@ -160,7 +157,7 @@ public function registerNotifier(IManager $notificationManager): void {
160157
}
161158

162159
public function createNotificationOnFirstLogin(IManager $notificationManager, IEventDispatcher $dispatcher): void {
163-
$dispatcher->addListener(UserFirstTimeLoggedInEvent::class, function(UserFirstTimeLoggedInEvent $event) use ($notificationManager) {
160+
$dispatcher->addListener(UserFirstTimeLoggedInEvent::class, function (UserFirstTimeLoggedInEvent $event) use ($notificationManager): void {
164161
$user = $event->getUser();
165162
$notification = $notificationManager->createNotification();
166163
$notification->setApp('terms_of_service')

lib/Checker.php

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
45
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -15,55 +16,22 @@
1516
use OCP\IURLGenerator;
1617
use OCP\IUser;
1718
use OCP\IUserSession;
18-
use OCP\IL10N;
1919
use Psr\Log\LoggerInterface;
2020

2121
class Checker {
22-
/** @var IRequest */
23-
private $request;
24-
/** @var IUserSession */
25-
private $userSession;
26-
/** @var ISession */
27-
private $session;
28-
/** @var SignatoryMapper */
29-
private $signatoryMapper;
30-
/** @var TermsMapper */
31-
private $termsMapper;
32-
/** @var CountryDetector */
33-
private $countryDetector;
34-
/** @var IConfig */
35-
private $config;
36-
/** @var IL10N */
37-
private $l10n;
38-
/** @var LoggerInterface */
39-
private $logger;
40-
/** @var array */
41-
private $termsCache = [];
42-
/** @var IURLGenerator */
43-
private $url;
22+
private array $termsCache = [];
4423

4524
public function __construct(
46-
IRequest $request,
47-
IUserSession $userSession,
48-
ISession $session,
49-
SignatoryMapper $signatoryMapper,
50-
TermsMapper $termsMapper,
51-
CountryDetector $countryDetector,
52-
IConfig $config,
53-
IL10N $l10n,
54-
LoggerInterface $logger,
55-
IURLGenerator $url
25+
private IRequest $request,
26+
private IUserSession $userSession,
27+
private ISession $session,
28+
private SignatoryMapper $signatoryMapper,
29+
private TermsMapper $termsMapper,
30+
private CountryDetector $countryDetector,
31+
private IConfig $config,
32+
private LoggerInterface $logger,
33+
private IURLGenerator $url,
5634
) {
57-
$this->request = $request;
58-
$this->userSession = $userSession;
59-
$this->session = $session;
60-
$this->signatoryMapper = $signatoryMapper;
61-
$this->termsMapper = $termsMapper;
62-
$this->countryDetector = $countryDetector;
63-
$this->config = $config;
64-
$this->l10n = $l10n;
65-
$this->logger = $logger;
66-
$this->url = $url;
6735
}
6836

6937
public function getForbiddenMessage(): string {
@@ -73,8 +41,6 @@ public function getForbiddenMessage(): string {
7341
/**
7442
* Whether the currently logged-in user has signed the terms and conditions
7543
* for the login action
76-
*
77-
* @return bool
7844
*/
7945
public function currentUserHasSigned(): bool {
8046
$uuid = $this->config->getAppValue(Application::APPNAME, 'term_uuid', '');
@@ -112,13 +78,11 @@ public function currentUserHasSigned(): bool {
11278
}
11379

11480
$signatories = $this->signatoryMapper->getSignatoriesByUser($user);
115-
if (!empty($signatories)) {
116-
foreach ($signatories as $signatory) {
117-
foreach ($this->termsCache[$countryCode] as $term) {
118-
if ((int)$term->getId() === (int)$signatory->getTermsId()) {
119-
$this->session->set('term_uuid', $uuid);
120-
return true;
121-
}
81+
foreach ($signatories as $signatory) {
82+
foreach ($this->termsCache[$countryCode] as $term) {
83+
if ((int)$term->getId() === (int)$signatory->getTermsId()) {
84+
$this->session->set('term_uuid', $uuid);
85+
return true;
12286
}
12387
}
12488
}
@@ -153,11 +117,11 @@ protected function isPathInfoStartingWith(string $allowedPath): bool {
153117
if ($allowedPath === '') {
154118
return false;
155119
}
156-
return strpos($this->request->getPathInfo(), $allowedPath) === 0;
120+
return str_starts_with($this->request->getPathInfo(), $allowedPath);
157121
}
158122

159123
protected function isAllowedScriptName(): bool {
160-
return substr($this->request->getScriptName(), 0 - strlen('/index.php')) === '/index.php';
124+
return str_ends_with($this->request->getScriptName(), '/index.php');
161125
}
162126

163127
protected function isRemoteAddressInRanges(array $allowedRanges): bool {
@@ -192,7 +156,7 @@ private function allowedRangeForApp(string $appId, string $configKey): array {
192156
* @copyright (IPv6) MW. https://stackoverflow.com/questions/7951061/matching-ipv6-address-to-a-cidr-subnet via
193157
*/
194158
private function matchCidr(string $ip, string $range): bool {
195-
list($subnet, $bits) = array_pad(explode('/', $range), 2, null);
159+
[$subnet, $bits] = array_pad(explode('/', $range), 2, null);
196160
if ($bits === null) {
197161
$bits = 32;
198162
}
@@ -211,23 +175,23 @@ private function matchCidr(string $ip, string $range): bool {
211175
$subnet = inet_pton($subnet);
212176
$ip = inet_pton($ip);
213177

214-
$binMask = str_repeat("f", (int)($bits / 4));
178+
$binMask = str_repeat('f', (int)($bits / 4));
215179
switch ($bits % 4) {
216180
case 0:
217181
break;
218182
case 1:
219-
$binMask .= "8";
183+
$binMask .= '8';
220184
break;
221185
case 2:
222-
$binMask .= "c";
186+
$binMask .= 'c';
223187
break;
224188
case 3:
225-
$binMask .= "e";
189+
$binMask .= 'e';
226190
break;
227191
}
228192

229193
$binMask = str_pad($binMask, 32, '0');
230-
$binMask = pack("H*", $binMask);
194+
$binMask = pack('H*', $binMask);
231195

232196
if (($ip & $binMask) === $subnet) {
233197
return true;
@@ -236,11 +200,11 @@ private function matchCidr(string $ip, string $range): bool {
236200
return false;
237201
}
238202

239-
private function isIpv4($ip) {
203+
private function isIpv4($ip): mixed {
240204
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
241205
}
242206

243-
private function isIpv6($ip) {
207+
private function isIpv6($ip): mixed {
244208
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
245209
}
246210
}

0 commit comments

Comments
 (0)