Skip to content

Commit 26f458c

Browse files
committed
Use fn and object type hint
1 parent 2e8566f commit 26f458c

5 files changed

Lines changed: 17 additions & 24 deletions

File tree

src/main/php/web/frontend/Exceptions.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class Exceptions implements Errors {
3333
*/
3434
public function catch($type, $handler= null) {
3535
if (null === $handler) {
36-
$this->mapping[$type]= function($cause) { return View::error($cause instanceof Error ? $cause->status() : 500); };
36+
$this->mapping[$type]= fn($cause) => View::error($cause instanceof Error ? $cause->status() : 500);
3737
} else if (is_int($handler)) {
38-
$this->mapping[$type]= function() use($handler) { return View::error($handler); };
38+
$this->mapping[$type]= fn($cause) => View::error($handler);
3939
} else if (is_callable($handler)) {
4040
$this->mapping[$type]= $handler;
4141
} else {

src/main/php/web/frontend/HandlersIn.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function __construct($package, $new= null) {
2323
$this->with($new ? $new($type->class()) : $type->newInstance(), (string)$handler->argument(0));
2424
}
2525
}
26-
uksort($this->patterns, function($a, $b) { return strlen($b) - strlen($a); });
26+
uksort($this->patterns, fn($a, $b) => strlen($b) - strlen($a));
2727
}
2828
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
<?php namespace web\frontend;
22

3-
use lang\{IllegalArgumentException, Reflection};
3+
use lang\Reflection;
44

55
/** Creates routing based on a given instance */
66
class MethodsIn extends Delegates {
77

8-
/** @param object $instance */
9-
public function __construct($instance) {
10-
if (!is_object($instance)) {
11-
throw new IllegalArgumentException('Expected an object, have '.typeof($instance));
12-
}
13-
8+
/** Creates with a given object */
9+
public function __construct(object $instance) {
1410
$type= Reflection::type($instance);
1511
if ($handler= $type->annotation(Handler::class)) {
1612
$this->with($instance, (string)$handler->argument(0));
1713
} else {
1814
$this->with($instance, '/');
1915
}
20-
uksort($this->patterns, function($a, $b) { return strlen($b) - strlen($a); });
16+
uksort($this->patterns, fn($a, $b) => strlen($b) - strlen($a));
2117
}
2218
}

src/main/php/xp/frontend/Resolver.class.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @test web.frontend.unittest.bundler.ResolverTest
2020
*/
2121
class Resolver {
22-
const LATEST = ['', '*', 'latest'];
22+
const LATEST= ['', '*', 'latest'];
2323

2424
private $fetch, $registry;
2525

@@ -38,14 +38,11 @@ public function __construct(Fetch $fetch, string $registry= 'https://data.jsdeli
3838
* @return [:var]
3939
*/
4040
private function select($versions, $lo, $hi) {
41-
$compare= function($id) use($lo, $hi) {
42-
return
43-
3 === sscanf($id, "%*d.%*d.%*d%[^\r]", $extra) &&
44-
version_compare($id, $lo, 'ge') &&
45-
version_compare($id, $hi, 'lt')
46-
;
47-
};
48-
return array_filter($versions, $compare);
41+
return array_filter($versions, fn($id) => (
42+
3 === sscanf($id, "%*d.%*d.%*d%[^\r]", $extra) &&
43+
version_compare($id, $lo, 'ge') &&
44+
version_compare($id, $hi, 'lt')
45+
));
4946
}
5047

5148
/**
@@ -59,7 +56,7 @@ public function version(string $library, string $constraint): string {
5956
if (in_array($constraint, self::LATEST)) {
6057
$candidates= array_filter(
6158
$info['versions'],
62-
function($id) { return 3 === sscanf($id, "%*d.%*d.%*d%[^\r]", $extra); }
59+
fn($id) => 3 === sscanf($id, "%*d.%*d.%*d%[^\r]", $extra)
6360
);
6461
} else if ('^' === $constraint[0]) { // Don't allow breaking changes
6562
$c= sscanf($constraint, '^%d.%d.%d');
@@ -89,7 +86,7 @@ function($id) { return 3 === sscanf($id, "%*d.%*d.%*d%[^\r]", $extra); }
8986

9087
// Find newest applicable version
9188
if ($candidates) {
92-
usort($candidates, function($a, $b) { return version_compare($b, $a); });
89+
usort($candidates, fn($a, $b) => version_compare($b, $a));
9390
return $candidates[0];
9491
}
9592

src/test/php/web/frontend/unittest/FrontendTest.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace web\frontend\unittest;
22

3-
use lang\IllegalArgumentException;
3+
use lang\Error;
44
use test\{Assert, Before, Expect, Test, Values};
55
use web\frontend\unittest\actions\Users;
66
use web\frontend\{Delegate, Exceptions, Frontend, RaiseErrors, Security, Templates, MethodsIn};
@@ -20,7 +20,7 @@ public function can_create() {
2020
new Frontend(new Users(), $this->templates);
2121
}
2222

23-
#[Test, Expect(IllegalArgumentException::class)]
23+
#[Test, Expect(Error::class)]
2424
public function first_argument_must_be_object() {
2525
new Frontend(null, $this->templates);
2626
}

0 commit comments

Comments
 (0)