Skip to content

Commit

Permalink
Modernise PHP code
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence committed May 6, 2024
1 parent 1d2203c commit 388a504
Show file tree
Hide file tree
Showing 58 changed files with 191 additions and 435 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/config',
__DIR__ . '/resources',
__DIR__ . '/src',
__DIR__ . '/tests',
])
// uncomment to reach your current PHP version
// ->withPhpSets()
->withPhpSets(php82: true)
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
])
->withSkip([
ClosureToarrowFunctionRector::class => [
__DIR__ . '/tests',
],
]);
20 changes: 7 additions & 13 deletions src/CodeExamples/CodeInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ public function getCodeFrom($callback): string
$reflection->getStartLine(),
$reflection->getEndLine() - $reflection->getStartLine() - 1
)
->pipe(function ($collection) {
return $this->removeExternalIndentation($collection);
})
->pipe(function ($collection) {
return $this->removeReturnKeyword($collection);
})
->pipe(fn($collection) => $this->removeExternalIndentation($collection))
->pipe(fn($collection) => $this->removeReturnKeyword($collection))
->implode("\n");
}

Expand All @@ -34,9 +30,7 @@ private function removeExternalIndentation(Collection $lines)
{
$leadingSpacesInFirstLine = $this->numberOfLeadingSpaces($lines->first());

return $lines->transform(function ($line) use ($leadingSpacesInFirstLine) {
return preg_replace("/^( {{$leadingSpacesInFirstLine}})/", '', $line);
});
return $lines->transform(fn($line) => preg_replace("/^( {{$leadingSpacesInFirstLine}})/", '', (string) $line));
}

private function numberOfLeadingSpaces(string $str)
Expand All @@ -51,12 +45,12 @@ private function numberOfLeadingSpaces(string $str)
*/
private function removeReturnKeyword(Collection $lines)
{
if (strpos($lines->first(), 'return ') === 0) {
return $lines->prepend(substr($lines->shift(), 7));
if (str_starts_with((string) $lines->first(), 'return ')) {
return $lines->prepend(substr((string) $lines->shift(), 7));
}

if (strpos($lines->last(), 'return ') === 0) {
return $lines->add(substr($lines->pop(), 7));
if (str_starts_with((string) $lines->last(), 'return ')) {
return $lines->add(substr((string) $lines->pop(), 7));
}

return $lines;
Expand Down
28 changes: 9 additions & 19 deletions src/CodeExamples/CodeResultExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,15 @@ private function exportValue($value): string
if (isset($value[ExampleSnippet::CLASS_NAME])) {
return $this->exportObject($value);
}

switch (gettype($value)) {
case 'array':
return $this->exportArray($value);
case 'integer':
return $this->format->integer($value);
case 'double':
case 'float':
return $this->format->float($value);
case 'string':
return $this->format->string($value);
case 'boolean':
return $this->format->bool($value ? 'true' : 'false');
case 'NULL':
case 'null':
return $this->format->null();
}

return '';
return match (gettype($value)) {
'array' => $this->exportArray($value),
'integer' => $this->format->integer($value),
'double', 'float' => $this->format->float($value),
'string' => $this->format->string($value),
'boolean' => $this->format->bool($value ? 'true' : 'false'),
'NULL', 'null' => $this->format->null(),
default => '',
};
}

private function exportArray($items): string
Expand Down
20 changes: 8 additions & 12 deletions src/CodeExamples/CodeResultTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ private function transformInArray($result, int $currentLevel = 0)
return $result;
}

return array_map(function ($item) use ($currentLevel) {
return $this->transformInArray($item, $currentLevel);
}, $result);
return array_map(fn($item) => $this->transformInArray($item, $currentLevel), $result);
}

private function exportFunction($result): array
Expand All @@ -56,21 +54,19 @@ private function exportFunction($result): array
private function exportParameters(array $parameters): array
{
return collect($parameters)
->map(function (ReflectionParameter $parameter) {
return [
ExampleSnippet::TYPE => $parameter->hasType() ? $parameter->getType()->getName() : null,
ExampleSnippet::PARAMETER => $parameter->getName(),
ExampleSnippet::OPTIONAL => $parameter->isOptional(),
ExampleSnippet::DEFAULT => $parameter->isOptional() ? $parameter->getDefaultValue() : null,
];
})
->map(fn(ReflectionParameter $parameter) => [
ExampleSnippet::TYPE => $parameter->hasType() ? $parameter->getType()->getName() : null,
ExampleSnippet::PARAMETER => $parameter->getName(),
ExampleSnippet::OPTIONAL => $parameter->isOptional(),
ExampleSnippet::DEFAULT => $parameter->isOptional() ? $parameter->getDefaultValue() : null,
])
->all();
}

private function exportObject(object $result, int $currentLevel): array
{
return [
ExampleSnippet::CLASS_NAME => get_class($result),
ExampleSnippet::CLASS_NAME => $result::class,
ExampleSnippet::ATTRIBUTES => $this->exportAttributes($result, $currentLevel),
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ExportDocumentationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ private function normalizeBaseUrl($url): string
return $url;
}

return '/'.ltrim($url, '/');
return '/'.ltrim((string) $url, '/');
}
}
20 changes: 7 additions & 13 deletions src/Console/DocumentationExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,11 @@ private function getSearchItems(Run $run)
{
return $run->groups
->load('examples')
->flatMap(function ($group) {
return $group->examples->map(function ($example) use ($group) {
return [
'section' => "{$group->area_title} / {$group->title}",
'title' => $example->title,
'url' => $this->getStaticUrl($example->url),
];
});
})
->flatMap(fn($group) => $group->examples->map(fn($example) => [
'section' => "{$group->area_title} / {$group->title}",
'title' => $example->title,
'url' => $this->getStaticUrl($example->url),
]))
->sortBy('title')
->values();
}
Expand Down Expand Up @@ -133,14 +129,12 @@ private function replaceUrls(string $contents)
$contents = preg_replace('@fetch\((.*?)search.json\'\)@', "fetch('{$this->staticBaseUrl}/search.json')", $contents);

// Assets paths
$contents = str_replace('/vendor/enlighten/', "{$this->staticBaseUrl}/assets/", $contents);
$contents = str_replace('/vendor/enlighten/', "{$this->staticBaseUrl}/assets/", (string) $contents);

// Internal links
return preg_replace_callback(
'@'.$this->originalBaseUrl.'([^"]+)?@',
function ($matches) {
return $this->getStaticUrl($matches[0]);
},
fn($matches) => $this->getStaticUrl($matches[0]),
$contents
);
}
Expand Down
8 changes: 1 addition & 7 deletions src/Drivers/DatabaseExampleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

class DatabaseExampleBuilder extends BaseExampleBuilder
{
/**
* @var DatabaseExampleGroupBuilder
*/
private $exampleGroupBuilder;

/**
* @var Example|null
*/
Expand All @@ -34,10 +29,9 @@ class DatabaseExampleBuilder extends BaseExampleBuilder
*/
private $currentSnippet = null;

public function __construct(DatabaseExampleGroupBuilder $exampleGroupBuilder)
public function __construct(private readonly DatabaseExampleGroupBuilder $exampleGroupBuilder)
{
$this->currentRequests = new Collection;
$this->exampleGroupBuilder = $exampleGroupBuilder;
}

public function addRequest(RequestInfo $request): void
Expand Down
8 changes: 1 addition & 7 deletions src/Drivers/DatabaseExampleGroupBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@

class DatabaseExampleGroupBuilder extends BaseExampleGroupBuilder
{
/**
* @var DatabaseRunBuilder
*/
private $runBuilder;

/**
* @var ExampleGroup|null
*/
protected $exampleGroup = null;

public function __construct(DatabaseRunBuilder $runBuilder)
public function __construct(private readonly DatabaseRunBuilder $runBuilder)
{
$this->runBuilder = $runBuilder;
}

public function save(): ExampleGroup
Expand Down
2 changes: 1 addition & 1 deletion src/Enlighten.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function test($keyOrCallback, $callback = null)

try {
return app(CodeExampleCreator::class)->createSnippet($callback, $key);
} catch (BindingResolutionException $exception) {
} catch (BindingResolutionException) {
throw new LaravelNotPresent;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ExampleCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ class ExampleCreator

protected ?ExampleBuilder $currentExampleBuilder = null;

protected ?Throwable $currentException;
protected ?Throwable $currentException = null;

public static function clearExampleGroupBuilder(): void
{
static::$currentExampleGroupBuilder = null;
}

public function __construct(
private RunBuilder $runBuilder,
private readonly RunBuilder $runBuilder,
protected Annotations $annotations,
protected Settings $settings,
private ExampleProfile $profile,
private readonly ExampleProfile $profile,
) {
}

Expand Down
10 changes: 2 additions & 8 deletions src/ExceptionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@

class ExceptionInfo
{
/**
* @var Throwable
*/
private $exception;

public static function make(Throwable $exception): ExceptionInfo
{
return new self($exception);
}

public function __construct(Throwable $exception)
public function __construct(private readonly Throwable $exception)
{
$this->exception = $exception;
}

public function getClassName(): string
{
return get_class($this->exception);
return $this->exception::class;
}

public function getCode(): int
Expand Down
18 changes: 6 additions & 12 deletions src/Http/Controllers/ShowAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,11 @@ private function endpoints(Run $run, Area $area)

$endpoints = $requests
->groupBy('signature')
->map(function ($requests) {
return new Endpoint(
$requests->first()->request_method,
$requests->first()->route_or_path,
$requests->unique(function ($response) {
return $response->signature.$response->example->slug;
})->sortBy('example.order')
);
})
->map(fn($requests) => new Endpoint(
$requests->first()->request_method,
$requests->first()->route_or_path,
$requests->unique(fn($response) => $response->signature.$response->example->slug)->sortBy('example.order')
))
->sortBy('method_index');

return view('enlighten::area.endpoints', [
Expand Down Expand Up @@ -113,9 +109,7 @@ private function getGroups(Run $run, Area $area): Collection
// because we use them to build the menu. So by filtering
// at a collection level we're actually saving a query.
return $run->groups
->when($area->isNotDefault(), function ($collection) use ($area) {
return $collection->where('area', $area->slug);
})
->when($area->isNotDefault(), fn($collection) => $collection->where('area', $area->slug))
->sortBy('order');
}

Expand Down
39 changes: 2 additions & 37 deletions src/HttpExamples/HttpExampleCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,8 @@ class HttpExampleCreator
*/
private static $followsRedirect = false;

/**
* @var ExampleCreator
*/
private $exampleCreator;

/**
* @var RequestInspector
*/
private $requestInspector;

/**
* @var ResponseInspector
*/
private $responseInspector;

/**
* @var SessionInspector
*/
private $sessionInspector;

/**
* @var RouteInspector
*/
private $routeInspector;

public function __construct(
ExampleCreator $exampleCreator,
RequestInspector $requestInspector,
RouteInspector $routeInspector,
ResponseInspector $responseInspector,
SessionInspector $sessionInspector
) {
$this->exampleCreator = $exampleCreator;
$this->requestInspector = $requestInspector;
$this->routeInspector = $routeInspector;
$this->responseInspector = $responseInspector;
$this->sessionInspector = $sessionInspector;
public function __construct(private readonly ExampleCreator $exampleCreator, private readonly RequestInspector $requestInspector, private readonly RouteInspector $routeInspector, private readonly ResponseInspector $responseInspector, private readonly SessionInspector $sessionInspector)
{
}

public static function followingRedirect(Closure $callback)
Expand Down
8 changes: 1 addition & 7 deletions src/HttpExamples/HttpExampleCreatorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@

class HttpExampleCreatorMiddleware
{
/**
* @var HttpExampleCreator
*/
private $httpExampleCreator;

public function __construct(HttpExampleCreator $httpExampleCreator)
public function __construct(private readonly HttpExampleCreator $httpExampleCreator)
{
$this->httpExampleCreator = $httpExampleCreator;
}

public function handle($request, Closure $next)
Expand Down
Loading

0 comments on commit 388a504

Please sign in to comment.