Skip to content

Commit

Permalink
improved coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 19, 2015
1 parent e07f521 commit 9abc5c3
Show file tree
Hide file tree
Showing 57 changed files with 494 additions and 485 deletions.
12 changes: 6 additions & 6 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ class Application extends Nette\Object
/** @var string */
public $errorPresenter;

/** @var callable[] function(Application $sender); Occurs before the application loads presenter */
/** @var callable[] function (Application $sender); Occurs before the application loads presenter */
public $onStartup;

/** @var callable[] function(Application $sender, \Exception $e = NULL); Occurs before the application shuts down */
/** @var callable[] function (Application $sender, \Exception $e = NULL); Occurs before the application shuts down */
public $onShutdown;

/** @var callable[] function(Application $sender, Request $request); Occurs when a new request is received */
/** @var callable[] function (Application $sender, Request $request); Occurs when a new request is received */
public $onRequest;

/** @var callable[] function(Application $sender, Presenter $presenter); Occurs when a presenter is created */
/** @var callable[] function (Application $sender, Presenter $presenter); Occurs when a presenter is created */
public $onPresenter;

/** @var callable[] function(Application $sender, IResponse $response); Occurs when a new response is ready for dispatch */
/** @var callable[] function (Application $sender, IResponse $response); Occurs when a new response is ready for dispatch */
public $onResponse;

/** @var callable[] function(Application $sender, \Exception $e); Occurs when an unhandled exception occurs in the application */
/** @var callable[] function (Application $sender, \Exception $e); Occurs when an unhandled exception occurs in the application */
public $onError;

/** @var Request[] */
Expand Down
4 changes: 2 additions & 2 deletions src/Application/PresenterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class PresenterFactory extends Nette\Object implements IPresenterFactory


/**
* @param callable function(string $class): IPresenter
* @param callable function (string $class): IPresenter
*/
public function __construct($factory = NULL)
{
$this->factory = $factory ?: function($class) { return new $class; };
$this->factory = $factory ?: function ($class) { return new $class; };
}


Expand Down
41 changes: 23 additions & 18 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class Presenter extends Control implements Application\IPresenter
/** @var int */
public $invalidLinkMode;

/** @var callable[] function(Presenter $sender, IResponse $response = NULL); Occurs when the presenter is shutting down */
/** @var callable[] function (Presenter $sender, IResponse $response = NULL); Occurs when the presenter is shutting down */
public $onShutdown;

/** @var Nette\Application\Request */
Expand Down Expand Up @@ -225,17 +225,20 @@ public function run(Application\Request $request)

} catch (Application\AbortException $e) {
// continue with shutting down
if ($this->isAjax()) try {
$hasPayload = (array) $this->payload; unset($hasPayload['state']);
if ($this->response instanceof Responses\TextResponse && $this->isControlInvalid()) {
$this->snippetMode = TRUE;
$this->response->send($this->httpRequest, $this->httpResponse);
$this->sendPayload();

} elseif (!$this->response && $hasPayload) { // back compatibility for use terminate() instead of sendPayload()
if ($this->isAjax()) {
try {
$hasPayload = (array) $this->payload;
unset($hasPayload['state']);
if ($this->response instanceof Responses\TextResponse && $this->isControlInvalid()) {
$this->snippetMode = TRUE;
$this->response->send($this->httpRequest, $this->httpResponse);
$this->sendPayload();
} elseif (!$this->response && $hasPayload) { // back compatibility for use terminate() instead of sendPayload()
$this->sendPayload();
}
} catch (Application\AbortException $e) {
}
} catch (Application\AbortException $e) { }
}

if ($this->hasFlashSession()) {
$this->getFlashSession()->setExpiration($this->response instanceof Responses\RedirectResponse ? '+ 30 seconds' : '+ 3 seconds');
Expand Down Expand Up @@ -314,7 +317,8 @@ public function processSignal()

try {
$component = $this->signalReceiver === '' ? $this : $this->getComponent($this->signalReceiver, FALSE);
} catch (Nette\InvalidArgumentException $e) {}
} catch (Nette\InvalidArgumentException $e) {
}

if (isset($e) || $component === NULL) {
throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not found.", NULL, isset($e) ? $e : NULL);
Expand Down Expand Up @@ -602,7 +606,7 @@ public function sendPayload()

/**
* Sends JSON data to the output.
* @param mixed $data
* @param mixed
* @return void
* @throws Nette\Application\AbortException
*/
Expand Down Expand Up @@ -735,7 +739,8 @@ public function canonicalize()
if (!$this->isAjax() && ($this->request->isMethod('get') || $this->request->isMethod('head'))) {
try {
$url = $this->createRequest($this, $this->action, $this->getGlobalState() + $this->request->getParameters(), 'redirectX');
} catch (InvalidLinkException $e) {}
} catch (InvalidLinkException $e) {
}
if (isset($url) && !$this->httpRequest->getUrl()->isEqual($url)) {
$this->sendResponse(new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY));
}
Expand Down Expand Up @@ -806,21 +811,21 @@ protected function createRequest($component, $destination, array $args, $mode)
}

// 4) signal or empty
if (!$component instanceof Presenter || substr($destination, -1) === '!') {
if (!$component instanceof self || substr($destination, -1) === '!') {
$signal = rtrim($destination, '!');
$a = strrpos($signal, ':');
if ($a !== FALSE) {
$component = $component->getComponent(strtr(substr($signal, 0, $a), ':', '-'));
$signal = (string) substr($signal, $a + 1);
}
if ($signal == NULL) { // intentionally ==
throw new InvalidLinkException("Signal must be non-empty string.");
throw new InvalidLinkException('Signal must be non-empty string.');
}
$destination = 'this';
}

if ($destination == NULL) { // intentionally ==
throw new InvalidLinkException("Destination must be non-empty string.");
throw new InvalidLinkException('Destination must be non-empty string.');
}

// 5) presenter: action
Expand Down Expand Up @@ -1314,7 +1319,7 @@ public function injectPrimary(Nette\DI\Container $context = NULL, Application\IP
Http\IRequest $httpRequest, Http\IResponse $httpResponse, Http\Session $session = NULL, Nette\Security\User $user = NULL, ITemplateFactory $templateFactory = NULL)
{
if ($this->presenterFactory !== NULL) {
throw new Nette\InvalidStateException("Method " . __METHOD__ . " is intended for initialization and should not be called more than once.");
throw new Nette\InvalidStateException('Method ' . __METHOD__ . ' is intended for initialization and should not be called more than once.');
}

$this->context = $context;
Expand Down Expand Up @@ -1368,7 +1373,7 @@ public function getSession($namespace = NULL)
{
if (!$this->session) {
throw new Nette\InvalidStateException('Service Session has not been set.');
}
}
return $namespace === NULL ? $this->session : $this->session->getSection($namespace);
}

Expand Down
14 changes: 8 additions & 6 deletions src/Application/UI/PresenterComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ public function hasCallableMethod($method)
{
$class = $this->getName();
$cache = & self::$mcCache[strtolower($class . ':' . $method)];
if ($cache === NULL) try {
$cache = FALSE;
$rm = new \ReflectionMethod($class, $method);
$cache = $this->isInstantiable() && $rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic();
} catch (\ReflectionException $e) {
if ($cache === NULL) {
try {
$cache = FALSE;
$rm = new \ReflectionMethod($class, $method);
$cache = $this->isInstantiable() && $rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic();
} catch (\ReflectionException $e) {
}
}
return $cache;
}
Expand All @@ -123,7 +125,7 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args)
if (isset($args[$name])) { // NULLs are ignored
$res[$i++] = $args[$name];
$type = $param->isArray() ? 'array' : ($param->isDefaultValueAvailable() ? gettype($param->getDefaultValue()) : 'NULL');
if (!self::convertType($res[$i-1], $type)) {
if (!self::convertType($res[$i - 1], $type)) {
$mName = $method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' . $method->getName() : $method->getName();
throw new BadRequestException("Invalid value for parameter '$name' in method $mName(), expected " . ($type === 'NULL' ? 'scalar' : $type) . ".");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationDI/ApplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function findPresenters()
$classFile = dirname($rc->getFileName()) . '/autoload_classmap.php';
if (is_file($classFile)) {
$this->getContainerBuilder()->addDependency($classFile);
$classes = array_merge($classes, array_keys(call_user_func(function($path) {
$classes = array_merge($classes, array_keys(call_user_func(function ($path) {
return require $path;
}, $classFile)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/ApplicationDI/LatteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function loadConfiguration()
$config = $this->validateConfig($this->defaults);
$container = $this->getContainerBuilder();

$latteFactory = $container->addDefinition($this->prefix('latteFactory'))
$container->addDefinition($this->prefix('latteFactory'))
->setClass('Latte\Engine')
->addSetup('setTempDirectory', [$this->tempDir])
->addSetup('setAutoRefresh', [$this->debugMode])
Expand Down Expand Up @@ -83,7 +83,7 @@ public function addMacro($macro)

$container = $this->getContainerBuilder();
$container->getDefinition($this->prefix('latteFactory'))
->addSetup('?->onCompile[] = function($engine) { ' . $macro . '($engine->getCompiler()); }', ['@self']);
->addSetup('?->onCompile[] = function ($engine) { ' . $macro . '($engine->getCompiler()); }', ['@self']);
}

}
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationDI/RoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function beforeCompile()

if ($this->debugMode && $this->config['debugger'] && $application = $container->getByType('Nette\Application\Application')) {
$container->getDefinition($application)->addSetup('@Tracy\Bar::addPanel', [
new Nette\DI\Statement('Nette\Bridges\ApplicationTracy\RoutingPanel')
new Nette\DI\Statement('Nette\Bridges\ApplicationTracy\RoutingPanel'),
]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationLatte/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function registerHelperLoader($loader)
{
trigger_error(__METHOD__ . '() is deprecated, use dynamic getLatte()->addFilter().', E_USER_DEPRECATED);
$latte = $this->latte;
$this->latte->addFilter(NULL, function($name) use ($loader, $latte) {
$this->latte->addFilter(NULL, function ($name) use ($loader, $latte) {
if ($callback = call_user_func($loader, $name)) {
$latte->addFilter($name, $callback);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function createTemplate(UI\Control $control = NULL)
$latte->onCompile = iterator_to_array($latte->onCompile);
}

array_unshift($latte->onCompile, function($latte) use ($control, $template) {
array_unshift($latte->onCompile, function ($latte) use ($control, $template) {
$latte->getParser()->shortNoEscape = TRUE;
$latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler()));
UIMacros::install($latte->getCompiler());
Expand All @@ -82,11 +82,11 @@ public function createTemplate(UI\Control $control = NULL)
foreach (['normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse'] as $name) {
$latte->addFilter($name, 'Nette\Utils\Strings::' . $name);
}
$latte->addFilter('null', function() {});
$latte->addFilter('length', function($var) {
$latte->addFilter('null', function () {});
$latte->addFilter('length', function ($var) {
return is_string($var) ? Nette\Utils\Strings::length($var) : count($var);
});
$latte->addFilter('modifyDate', function($time, $delta, $unit = NULL) {
$latte->addFilter('modifyDate', function ($time, $delta, $unit = NULL) {
return $time == NULL ? NULL : Nette\Utils\DateTime::from($time)->modify($delta . $unit); // intentionally ==
});

Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationLatte/UIMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function install(Latte\Compiler $compiler)
$me = new static($compiler);
$me->addMacro('control', [$me, 'macroControl']);

$me->addMacro('href', NULL, NULL, function(MacroNode $node, PhpWriter $writer) use ($me) {
$me->addMacro('href', NULL, NULL, function (MacroNode $node, PhpWriter $writer) use ($me) {
return ' ?> href="<?php ' . $me->macroLink($node, $writer) . ' ?>"<?php ';
});
$me->addMacro('plink', [$me, 'macroLink']);
Expand Down
6 changes: 3 additions & 3 deletions src/Bridges/ApplicationTracy/RoutingPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class RoutingPanel extends Nette\Object implements Tracy\IBarPanel

public static function initializePanel(Nette\Application\Application $application)
{
Tracy\Debugger::getBlueScreen()->addPanel(function($e) use ($application) {
Tracy\Debugger::getBlueScreen()->addPanel(function ($e) use ($application) {
return $e ? NULL : [
'tab' => 'Nette Application',
'panel' => '<h3>Requests</h3>' . Dumper::toHtml($application->getRequests(), [Dumper::LIVE => TRUE])
. '<h3>Presenter</h3>' . Dumper::toHtml($application->getPresenter(), [Dumper::LIVE => TRUE])
. '<h3>Presenter</h3>' . Dumper::toHtml($application->getPresenter(), [Dumper::LIVE => TRUE]),
];
});
}
Expand Down Expand Up @@ -122,7 +122,7 @@ private function analyse($router, $module = '')
'defaults' => $router instanceof Routers\Route || $router instanceof Routers\SimpleRouter ? $router->getDefaults() : [],
'mask' => $router instanceof Routers\Route ? $router->getMask() : NULL,
'request' => $request,
'module' => rtrim($module, ':')
'module' => rtrim($module, ':'),
];
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Application.DI/ApplicationExtension.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use Nette\DI,
require __DIR__ . '/../bootstrap.php';


test(function() {
test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('application', new ApplicationExtension(FALSE));

Expand All @@ -27,7 +27,7 @@ test(function() {
eval($code);

$container = new Container1;
Assert::type( 'Nette\Application\Application', $container->getService('application') );
Assert::type( 'Nette\Application\PresenterFactory', $container->getService('nette.presenterFactory') );
Assert::type( 'Nette\Application\LinkGenerator', $container->getService('application.linkGenerator') );
Assert::type('Nette\Application\Application', $container->getService('application'));
Assert::type('Nette\Application\PresenterFactory', $container->getService('nette.presenterFactory'));
Assert::type('Nette\Application\LinkGenerator', $container->getService('application.linkGenerator'));
});
8 changes: 4 additions & 4 deletions tests/Application.DI/ApplicationExtension.invalidLink.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function createCompiler($config)
}


test(function() {
test(function () {
$compiler = createCompiler('
application:
debugger: no
Expand All @@ -47,7 +47,7 @@ test(function() {
});


test(function() {
test(function () {
$compiler = createCompiler('
application:
debugger: no
Expand All @@ -68,7 +68,7 @@ test(function() {
});


test(function() {
test(function () {
$compiler = createCompiler('
application:
debugger: no
Expand All @@ -89,7 +89,7 @@ test(function() {
});


test(function() {
test(function () {
$compiler = createCompiler('
application:
debugger: no
Expand Down
26 changes: 13 additions & 13 deletions tests/Application.DI/ApplicationExtension.scan.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/files/MyPresenter.php';


test(function() {
test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('application', new ApplicationExtension);

Expand All @@ -28,13 +28,13 @@ test(function() {

$container = new Container1;
$tags = $container->findByTag('nette.presenter');
Assert::count( 1, array_keys($tags, 'NetteModule\ErrorPresenter') );
Assert::count( 1, array_keys($tags, 'NetteModule\MicroPresenter') );
Assert::count( 0, array_keys($tags, 'Nette\Application\UI\Presenter') );
Assert::count(1, array_keys($tags, 'NetteModule\ErrorPresenter'));
Assert::count(1, array_keys($tags, 'NetteModule\MicroPresenter'));
Assert::count(0, array_keys($tags, 'Nette\Application\UI\Presenter'));
});


test(function() {
test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('application', new ApplicationExtension);

Expand All @@ -52,13 +52,13 @@ test(function() {

$container = new Container2;
$tags = $container->findByTag('nette.presenter');
Assert::count( 1, array_keys($tags, 'BasePresenter') );
Assert::count( 1, array_keys($tags, 'Presenter1') );
Assert::count( 1, array_keys($tags, 'Presenter2') );
Assert::count(1, array_keys($tags, 'BasePresenter'));
Assert::count(1, array_keys($tags, 'Presenter1'));
Assert::count(1, array_keys($tags, 'Presenter2'));
});


test(function() {
test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('application', new ApplicationExtension(FALSE, [__DIR__ . '/files']));

Expand All @@ -82,10 +82,10 @@ test(function() {

$container = new Container3;
$tags = $container->findByTag('nette.presenter');
Assert::count( 1, array_keys($tags, 'BasePresenter') );
Assert::count( 1, array_keys($tags, 'Presenter1') );
Assert::count( 1, array_keys($tags, 'Presenter2') );
Assert::count(1, array_keys($tags, 'BasePresenter'));
Assert::count(1, array_keys($tags, 'Presenter1'));
Assert::count(1, array_keys($tags, 'Presenter2'));

$tmp = array_keys($tags, 'Presenter1');
Assert::same( 'test', $container->getService($tmp[0])->getView() );
Assert::same('test', $container->getService($tmp[0])->getView());
});
Loading

0 comments on commit 9abc5c3

Please sign in to comment.