Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "php-cs-fixer" for code formatting #28

Merged
merged 4 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.php-cs-fixer.cache
.phpunit.result.cache
build
composer.lock
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to `Slytherin` will be documented in this file.

## [0.9.8](https://github.com/rougin/slytherin/compare/v0.9.7...v0.9.8) - Unreleased

### Added
- `phpcsfixer.php` as custom rules for the `friendsofphp/php-cs-fixer` package
- Test cases for the complete code coverage after running `friendsofphp/php-cs-fixer`

### Changed
- `Router::__construct` supports adding `RouteInterface` in the array
- Code styling based on `friendsofphp/php-cs-fixer` package

## [0.9.7](https://github.com/rougin/slytherin/compare/v0.9.6...v0.9.7) - 2024-01-12

### Added
Expand Down
39 changes: 39 additions & 0 deletions phpstyle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

// Specify the paths in this variable ---
$paths = array(__DIR__ . '/src');
$paths[] = realpath(__DIR__ . '/tests');
// --------------------------------------

// Specify the rules for code formatting ---------
$rules = array('@PSR12' => true);

$cscp = 'control_structure_continuation_position';
$rules[$cscp] = array('position' => 'next_line');

$braces = array();
$braces['control_structures_opening_brace'] = 'next_line_unless_newline_at_signature_end';
$braces['functions_opening_brace'] = 'next_line_unless_newline_at_signature_end';
$braces['anonymous_functions_opening_brace'] = 'next_line_unless_newline_at_signature_end';
$braces['anonymous_classes_opening_brace'] = 'next_line_unless_newline_at_signature_end';
$braces['allow_single_line_empty_anonymous_classes'] = false;
$braces['allow_single_line_anonymous_functions'] = false;

$rules['braces_position'] = $braces;

$rules['new_with_parentheses'] = array('named_class' => false);

$visibility = array('elements' => array());
$visibility['elements'] = array('method', 'property');
$rules['visibility_required'] = $visibility;
// -----------------------------------------------

$finder = new \PhpCsFixer\Finder;

$finder->in((array) $paths);

$config = new \PhpCsFixer\Config;

$config->setRules($rules);

return $config->setFinder($finder);
2 changes: 1 addition & 1 deletion src/Component/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getErrorHandler()
/**
* Returns the HTTP request and response.
*
* @return array<int, mixed>
* @return array<integer, mixed>
*/
public function getHttp()
{
Expand Down
7 changes: 5 additions & 2 deletions src/Component/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function make(ContainerInterface $container)

if ($item->getType() === 'http')
{
/** @var array<int, mixed> */
/** @var array<integer, mixed> */
$result = $item->get();

/** @var \Psr\Http\Message\ServerRequestInterface */
Expand Down Expand Up @@ -125,7 +125,10 @@ public static function get(array $components, ContainerInterface $container = nu
{
$self = new Collector($components);

if (! $container) $container = new Container;
if (! $container)
{
$container = new Container;
}

return $self->make($container);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Container/AurynContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public function get($id)
*/
public function has($id)
{
if (array_key_exists($id, $this->items)) return true;
if (array_key_exists($id, $this->items))
{
return true;
}

$filter = Injector::I_BINDINGS | Injector::I_DELEGATES;

Expand Down
5 changes: 4 additions & 1 deletion src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public function get($id)

$entry = $this->items[(string) $id];

if (is_object($entry)) return $entry;
if (is_object($entry))
{
return $entry;
}

$message = sprintf('Alias (%s) is not an object', $id);

Expand Down
5 changes: 4 additions & 1 deletion src/Container/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public function getClass()
$builtIn = call_user_func(array($type, 'isBuiltin'));
}

if ($builtIn) return null;
if ($builtIn)
{
return null;
}

/** @var callable */
$class = array($type, 'getName');
Expand Down
24 changes: 9 additions & 15 deletions src/Container/ReflectionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function get($id)
*
* @param \ReflectionFunctionAbstract $reflector
* @param array<string, mixed> $parameters
* @return array<int, mixed>
* @return array<integer, mixed>
*/
public function getArguments(\ReflectionFunctionAbstract $reflector, $parameters = array())
{
Expand All @@ -89,7 +89,10 @@ public function getArguments(\ReflectionFunctionAbstract $reflector, $parameters
$result[$key] = $parameters[$name];
}

if ($argument) $result[$key] = $argument;
if ($argument)
{
$result[$key] = $argument;
}
}

return $result;
Expand Down Expand Up @@ -131,9 +134,9 @@ protected function getArgument(\ReflectionParameter $parameter)
$param = new Parameter($parameter);
// --------------------------------------------------

$argument = null; $name = $param->getName();
$argument = null;

if ($this->has($name))
if ($this->has($name = $param->getName()))
{
$argument = $this->get((string) $name);
}
Expand All @@ -146,26 +149,17 @@ protected function getArgument(\ReflectionParameter $parameter)
* Resolves the specified parameters from a container.
*
* @param \ReflectionFunction|\ReflectionMethod $reflection
* @param array<string, mixed> $parameters
* @return array<int, mixed>
* @return array<integer, mixed>
*/
protected function resolve($reflection, $parameters = array())
protected function resolve($reflection)
{
$items = $reflection->getParameters();

$result = array();

foreach ($items as $key => $item)
{
// Backward compatibility for ReflectionParameter -------
$param = new Parameter($item); $name = $param->getName();
// ------------------------------------------------------

$result[$key] = $this->getArgument($item);

$exists = array_key_exists($name, $parameters);

if ($exists) $result[$key] = $parameters[$name];
}

return $result;
Expand Down
5 changes: 4 additions & 1 deletion src/Debug/ErrorHandlerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function define(ContainerInterface $container, Configuration $config)
/** @var string */
$environment = $config->get('app.environment', 'development');

if ($environment !== 'development') return $container;
if ($environment !== 'development')
{
return $container;
}

$handler = new ErrorHandler($environment);

Expand Down
2 changes: 1 addition & 1 deletion src/Http/HttpIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function define(ContainerInterface $container, Configuration $config)
* Returns the PHP's global variables.
*
* @param \Rougin\Slytherin\Integration\Configuration $config
* @return array<int, mixed>
* @return array<integer, mixed>
*/
protected function globals(Configuration $config)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Response extends Message implements ResponseInterface
protected $code = 200;

/**
* @var array<int, string>
* @var array<integer, string>
*/
protected $codes = array(
100 => 'Continue',
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ public function getMetadata($key = null)
*/
public function getSize()
{
if ($this->stream === null) return null;
if ($this->stream === null)
{
return null;
}

if (is_null($this->size))
{
Expand Down
4 changes: 1 addition & 3 deletions src/Http/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ public static function normalize(array $uploaded)
{
$items = array();

if (! isset($file['name'])) continue;

foreach ($file['name'] as $key => $value)
{
$items[] = self::create($file, $key);
Expand All @@ -177,7 +175,7 @@ public static function normalize(array $uploaded)
/**
* Creates a new UploadedFile instance.
*
* @param array<string, array<int, string|integer>> $file
* @param array<string, array<integer, string|integer>> $file
* @param integer $key
* @return \Psr\Http\Message\UploadedFileInterface
*/
Expand Down
4 changes: 1 addition & 3 deletions src/Http/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,7 @@ public function withUserInfo($user, $password = null)
*/
public static function instance(array $server)
{
$secure = 'off';

if (isset($server['HTTPS'])) $secure = $server['HTTPS'];
$secure = isset($server['HTTPS']) ? 'on' : 'off';

$http = $secure === 'off' ? 'http' : 'https';

Expand Down
12 changes: 9 additions & 3 deletions src/Integration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class Configuration
*/
public function __construct($data = null)
{
if (is_array($data)) $this->data = $data;
if (is_array($data))
{
$this->data = $data;
}

if (is_string($data))
{
Expand Down Expand Up @@ -63,7 +66,7 @@ public function get($key, $default = null)
*/
public function load($directory)
{
/** @var array<int, string> */
/** @var array<integer, string> */
$configurations = glob($directory . '/*.php');

foreach ($configurations as $item)
Expand Down Expand Up @@ -118,7 +121,10 @@ protected function save(array &$keys, &$data, $value)
return $data[$key];
}

if (! isset($data[$key])) $data[$key] = array();
if (! isset($data[$key]))
{
$data[$key] = array();
}

return $this->save($keys, $data[$key], $value);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Middleware/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class Dispatcher implements DispatcherInterface
*/
public function __construct($stack = array())
{
if ($stack) $this->setStack($stack);
if ($stack)
{
$this->setStack($stack);
}
}

/**
Expand Down Expand Up @@ -77,7 +80,10 @@ public function push($middleware)
return $this;
}

foreach ($middleware as $item) $this->push($item);
foreach ($middleware as $item)
{
$this->push($item);
}

return $this;
}
Expand Down
8 changes: 2 additions & 6 deletions src/Middleware/MiddlewareIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ class MiddlewareIntegration implements IntegrationInterface
*/
public function define(ContainerInterface $container, Configuration $config)
{
$middleware = System::MIDDLEWARE;

if (! interface_exists($middleware)) return $container;

/** @var array<int, mixed> */
/** @var array<integer, mixed> */
$stack = $config->get('app.middlewares', array());

$dispatch = new Dispatcher($stack);
Expand All @@ -54,6 +50,6 @@ public function define(ContainerInterface $container, Configuration $config)
$dispatch = new StratigilityDispatcher($pipe);
}

return $container->set($middleware, $dispatch);
return $container->set(System::MIDDLEWARE, $dispatch);
}
}
5 changes: 4 additions & 1 deletion src/Middleware/StratigilityDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ public function process(ServerRequestInterface $request, HandlerInterface $handl
*/
protected function setFactory(ResponseInterface $response)
{
if (! $this->hasFactory()) return;
if (! $this->hasFactory())
{
return;
}

/** @var class-string */
$factory = 'Zend\Stratigility\Middleware\CallableMiddlewareWrapperFactory';
Expand Down
5 changes: 4 additions & 1 deletion src/Middleware/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public function process(ServerRequestInterface $request, HandlerInterface $handl
{
$middleware = $this->middleware;

if (is_string($middleware)) $middleware = new $middleware;
if (is_string($middleware))
{
$middleware = new $middleware;
}

$next = Interop::getHandler($handler);

Expand Down
10 changes: 8 additions & 2 deletions src/Routing/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class Dispatcher implements DispatcherInterface
*/
public function __construct(RouterInterface $router = null)
{
if ($router) $this->setRouter($router);
if ($router)
{
$this->setRouter($router);
}
}

/**
Expand Down Expand Up @@ -99,7 +102,10 @@ public function setRouter(RouterInterface $router)
*/
protected function validMethod($method)
{
if (in_array($method, $this->allowed)) return true;
if (in_array($method, $this->allowed))
{
return true;
}

$message = 'Used method is not allowed (' . $method . ')';

Expand Down
2 changes: 1 addition & 1 deletion src/Routing/FastRouteDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function dispatch($method, $uri)
{
$this->validMethod($method);

/** @var array<int, int|string> */
/** @var array<integer, int|string> */
$result = $this->fastroute->dispatch($method, $uri);

if ($result[0] === \FastRoute\Dispatcher::NOT_FOUND)
Expand Down
Loading
Loading