Skip to content

Commit

Permalink
added PHP 7.1 scalar and return type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 25, 2017
1 parent 2051e3d commit 804688d
Show file tree
Hide file tree
Showing 56 changed files with 250 additions and 586 deletions.
29 changes: 8 additions & 21 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ public function __construct(IPresenterFactory $presenterFactory, IRouter $router

/**
* Dispatch a HTTP request to a front controller.
* @return void
*/
public function run()
public function run(): void
{
try {
$this->onStartup($this);
Expand All @@ -103,10 +102,7 @@ public function run()
}


/**
* @return Request
*/
public function createInitialRequest()
public function createInitialRequest(): Request
{
$request = $this->router->match($this->httpRequest);
if (!$request instanceof Request) {
Expand All @@ -116,10 +112,7 @@ public function createInitialRequest()
}


/**
* @return void
*/
public function processRequest(Request $request)
public function processRequest(Request $request): void
{
process:
if (count($this->requests) > self::$maxLoop) {
Expand Down Expand Up @@ -152,10 +145,7 @@ public function processRequest(Request $request)
}


/**
* @return void
*/
public function processException($e)
public function processException(\Throwable $e): void
{
if (!$e instanceof BadRequestException && $this->httpResponse instanceof Nette\Http\Response) {
$this->httpResponse->warnOnBuffer = FALSE;
Expand All @@ -181,17 +171,16 @@ public function processException($e)
* Returns all processed requests.
* @return Request[]
*/
public function getRequests()
public function getRequests(): array
{
return $this->requests;
}


/**
* Returns current presenter.
* @return IPresenter
*/
public function getPresenter()
public function getPresenter(): IPresenter
{
return $this->presenter;
}
Expand All @@ -202,19 +191,17 @@ public function getPresenter()

/**
* Returns router.
* @return IRouter
*/
public function getRouter()
public function getRouter(): IRouter
{
return $this->router;
}


/**
* Returns presenter factory.
* @return IPresenterFactory
*/
public function getPresenterFactory()
public function getPresenterFactory(): IPresenterFactory
{
return $this->presenterFactory;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Application/ErrorPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public function __construct(ILogger $logger = NULL)
}


/**
* @return Application\IResponse
*/
public function run(Application\Request $request)
public function run(Application\Request $request): Application\IResponse
{
$e = $request->getParameter('exception');
if ($e instanceof Application\BadRequestException) {
Expand Down
3 changes: 1 addition & 2 deletions src/Application/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class Helpers

/**
* Splits name into [module, presenter] or [presenter, action]
* @return array
*/
public static function splitName($name)
public static function splitName(string $name): array
{
$pos = strrpos($name, ':');
return $pos === FALSE
Expand Down
5 changes: 1 addition & 4 deletions src/Application/IPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
interface IPresenter
{

/**
* @return IResponse
*/
function run(Request $request);
function run(Request $request): IResponse;

}
5 changes: 2 additions & 3 deletions src/Application/IPresenterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ interface IPresenterFactory
* @return string class name
* @throws InvalidPresenterException
*/
function getPresenterClass(&$name);
function getPresenterClass(string &$name): string;

/**
* Creates new presenter instance.
* @param string presenter name
* @return IPresenter
*/
function createPresenter($name);
function createPresenter(string $name): IPresenter;

}
3 changes: 1 addition & 2 deletions src/Application/IResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ interface IResponse

/**
* Sends response to output.
* @return void
*/
function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse);
function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse): void;

}
6 changes: 2 additions & 4 deletions src/Application/IRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ interface IRouter

/**
* Maps HTTP request to a Request object.
* @return Request|NULL
*/
function match(Nette\Http\IRequest $httpRequest);
function match(Nette\Http\IRequest $httpRequest): ?Request;

/**
* Constructs absolute URL from Request object.
* @return string|NULL
*/
function constructUrl(Request $appRequest, Nette\Http\Url $refUrl);
function constructUrl(Request $appRequest, Nette\Http\Url $refUrl): ?string;

}
3 changes: 1 addition & 2 deletions src/Application/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ public function __construct(IRouter $router, Nette\Http\Url $refUrl, IPresenterF
/**
* Generates URL to presenter.
* @param string destination in format "[[[module:]presenter:]action] [#fragment]"
* @return string
* @throws UI\InvalidLinkException
*/
public function link($dest, array $params = [])
public function link(string $dest, array $params = []): string
{
if (!preg_match('~^([\w:]+):(\w*+)(#.*)?()\z~', $dest, $m)) {
throw new UI\InvalidLinkException("Invalid link destination '$dest'.");
Expand Down
29 changes: 8 additions & 21 deletions src/Application/MicroPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,14 @@ public function __construct(Nette\DI\Container $context = NULL, Http\IRequest $h

/**
* Gets the context.
* @return Nette\DI\Container
*/
public function getContext()
public function getContext(): Nette\DI\Container
{
return $this->context;
}


/**
* @return Nette\Application\IResponse
*/
public function run(Application\Request $request)
public function run(Application\Request $request): Nette\Application\IResponse
{
$this->request = $request;

Expand Down Expand Up @@ -109,10 +105,8 @@ public function run(Application\Request $request)

/**
* Template factory.
* @param string
* @return Application\UI\ITemplate
*/
public function createTemplate($class = NULL, callable $latteFactory = NULL)
public function createTemplate(string $class = NULL, callable $latteFactory = NULL): Application\UI\ITemplate
{
$latte = $latteFactory ? $latteFactory() : $this->getContext()->getByType(Nette\Bridges\ApplicationLatte\ILatteFactory::class)->create();
$template = $class ? new $class : new Nette\Bridges\ApplicationLatte\Template($latte);
Expand All @@ -131,33 +125,26 @@ public function createTemplate($class = NULL, callable $latteFactory = NULL)

/**
* Redirects to another URL.
* @param string
* @param int HTTP code
* @return Nette\Application\Responses\RedirectResponse
* @param int $code HTTP code
*/
public function redirectUrl($url, $code = Http\IResponse::S302_FOUND)
public function redirectUrl(string $url, int $code = Http\IResponse::S302_FOUND): Nette\Application\Responses\RedirectResponse
{
return new Responses\RedirectResponse($url, $code);
}


/**
* Throws HTTP error.
* @param string
* @param int HTTP error code
* @return void
* @param int $code HTTP error code
* @throws Nette\Application\BadRequestException
*/
public function error($message = NULL, $code = Http\IResponse::S404_NOT_FOUND)
public function error(string $message = NULL, int $code = Http\IResponse::S404_NOT_FOUND): void
{
throw new Application\BadRequestException($message, $code);
}


/**
* @return Nette\Application\Request
*/
public function getRequest()
public function getRequest(): Nette\Application\Request
{
return $this->request;
}
Expand Down
13 changes: 4 additions & 9 deletions src/Application/PresenterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public function __construct(callable $factory = NULL)
/**
* Creates new presenter instance.
* @param string presenter name
* @return IPresenter
*/
public function createPresenter($name)
public function createPresenter(string $name): IPresenter
{
return ($this->factory)($this->getPresenterClass($name));
}
Expand All @@ -58,7 +57,7 @@ public function createPresenter($name)
* @return string class name
* @throws InvalidPresenterException
*/
public function getPresenterClass(&$name)
public function getPresenterClass(string &$name): string
{
if (isset($this->cache[$name])) {
return $this->cache[$name];
Expand Down Expand Up @@ -117,11 +116,9 @@ public function setMapping(array $mapping)

/**
* Formats presenter class name from its name.
* @param string
* @return string
* @internal
*/
public function formatPresenterClass($presenter)
public function formatPresenterClass(string $presenter): string
{
$parts = explode(':', $presenter);
$mapping = isset($parts[1], $this->mapping[$parts[0]])
Expand All @@ -137,11 +134,9 @@ public function formatPresenterClass($presenter)

/**
* Formats presenter name from class name.
* @param string
* @return string|NULL
* @internal
*/
public function unformatPresenterClass($class)
public function unformatPresenterClass(string $class): ?string
{
foreach ($this->mapping as $module => $mapping) {
$mapping = str_replace(['\\', '*'], ['\\\\', '(\w+)'], $mapping);
Expand Down
Loading

0 comments on commit 804688d

Please sign in to comment.