Skip to content
Permalink
Browse files

added PHP 7 typehints

  • Loading branch information...
dg committed Oct 5, 2018
1 parent a3c177d commit c4a7c65276d32419a0136fe6aa23176e72decc27
Showing with 222 additions and 290 deletions.
  1. +3 −6 cs/access-control.texy
  2. +5 −6 cs/ajax.texy
  3. +3 −3 cs/arrays.texy
  4. +6 −12 cs/components.texy
  5. +10 −10 cs/dependency-injection.texy
  6. +10 −10 cs/di-configuration.texy
  7. +5 −8 cs/di-usage.texy
  8. +2 −2 cs/finder.texy
  9. +1 −1 cs/form-rendering.texy
  10. +3 −3 cs/form-validation.texy
  11. +5 −11 cs/forms.texy
  12. +1 −4 cs/localization.texy
  13. +1 −1 cs/mailing.texy
  14. +2 −2 cs/multiplier.texy
  15. +6 −6 cs/pagination.texy
  16. +4 −4 cs/presenters.texy
  17. +2 −2 cs/quickstart/authentication.texy
  18. +2 −2 cs/quickstart/comments.texy
  19. +2 −2 cs/quickstart/creating-posts.texy
  20. +2 −2 cs/quickstart/single-post.texy
  21. +8 −17 cs/routing.texy
  22. +9 −9 cs/smartobject.texy
  23. +1 −1 cs/strings.texy
  24. +3 −3 cs/tokenizer.texy
  25. +3 −6 en/access-control.texy
  26. +5 −6 en/ajax.texy
  27. +3 −3 en/arrays.texy
  28. +6 −12 en/components.texy
  29. +10 −10 en/dependency-injection.texy
  30. +10 −10 en/di-configuration.texy
  31. +5 −8 en/di-usage.texy
  32. +2 −2 en/finder.texy
  33. +1 −1 en/form-rendering.texy
  34. +3 −3 en/form-validation.texy
  35. +5 −11 en/forms.texy
  36. +1 −4 en/localization.texy
  37. +1 −1 en/mailing.texy
  38. +2 −2 en/multiplier.texy
  39. +6 −6 en/pagination.texy
  40. +4 −4 en/presenters.texy
  41. +2 −2 en/quickstart/authentication.texy
  42. +2 −2 en/quickstart/comments.texy
  43. +2 −2 en/quickstart/creating-posts.texy
  44. +2 −2 en/quickstart/single-post.texy
  45. +8 −17 en/routing.texy
  46. +9 −9 en/smartobject.texy
  47. +1 −1 en/strings.texy
  48. +3 −3 en/tokenizer.texy
  49. +1 −1 wip/book/cs/book/ajax.texy
  50. +9 −9 wip/book/cs/book/authentication.texy
  51. +7 −7 wip/book/cs/book/components.texy
  52. +3 −6 wip/book/cs/book/database.texy
  53. +5 −5 wip/book/cs/book/forms.texy
  54. +2 −2 wip/book/cs/book/presenter.texy
  55. +3 −6 wip/book/en/book/database.texy
@@ -111,7 +111,7 @@ class MyAuthenticator implements NS\IAuthenticator
$this->database = $database;
}

public function authenticate(array $credentials)
public function authenticate(array $credentials): Nette\Security\Identity
{
list($username, $password) = $credentials;
$row = $this->database->table('users')
@@ -215,7 +215,7 @@ Rámcová podoba implementace vypadá takto:
/--php
class MyAuthorizator implements Nette\Security\IAuthorizator
{
public function isAllowed($role, $resource, $privilege)
public function isAllowed($role, $resource, $privilege): bool
{
return ...; // vrací true nebo false
}
@@ -425,10 +425,7 @@ namespace App\Model;

class AuthorizatorFactory
{
/**
* @return Nette\Security\Permission
*/
public static function create()
public static function create(): Nette\Security\Permission
{
$acl = new Nette\Security\Permission;

@@ -19,7 +19,7 @@ Nette neobsahuje implementaci AJAX požadavků na straně klienta. Doporučujeme
Pro odesílání dat prohlížeči ve formátu JSON lze využít předpřipravený objekt `payload`:

/--php
public function actionDelete($id)
public function actionDelete(int $id)
{
if ($this->isAjax()) {
$this->payload->message = 'Success';
@@ -57,7 +57,7 @@ Invalidace
Každý objekt třídy [Control |components] (což je i samotný Presenter) si umí zapamatovat, jestli při subrequestu došlo ke změnám, které si vyžadují jej překreslit. K tomu slouží dvojice metod `redrawControl()` a `isControlInvalid()`. Příklad:

/--php
public function handleLogin($user)
public function handleLogin(string $user)
{
// po přihlášení uživatele se musí objekt překreslit
$this->redrawControl();
@@ -136,9 +136,8 @@ class HomepagePresenter extends Nette\Application\UI\Presenter
/**
* Zde je nějaká logika pro získání celého seznamu. Správně to patří do
* modelu, ale pro příklad to nebudu komplikovat.
* @return array
*/
private function getTheWholeList()
private function getTheWholeList(): array
{
return [
'První',
@@ -154,7 +153,7 @@ class HomepagePresenter extends Nette\Application\UI\Presenter
}
}

public function handleUpdate($id)
public function handleUpdate(int $id)
{
$this->template->list = $this->isAjax()
? []
@@ -240,7 +239,7 @@ $.getJSON(
A handle metoda s odpovídajícími parametry v komponentě.

/--php
public function handleChangeCountBasket($id, $count)
public function handleChangeCountBasket(int $id, int $count)
{

}
@@ -202,7 +202,7 @@ Otestuje, zda-li nějaký prvek v poli odpovídá podmínce v `$callback`, kter
/--php
$array = ['foo', 'bar', 'baz'];
// have some bar???
$res = Arrays::some($array, function ($value) { return $value === 'bar'; });
$res = Arrays::some($array, function ($value): bool { return $value === 'bar'; });
// $res = true
\--

@@ -215,7 +215,7 @@ Otestuje, zda-li všechny prvky v poli odpovídají podmínce v `$callback`, kte
/--php
$array = ['foo', 'bar', 'baz'];
// have only bar???
$res = Arrays::every($array, function ($value) { return $value === 'bar'; });
$res = Arrays::every($array, function ($value): bool { return $value === 'bar'; });
// $res = false
\--

@@ -227,7 +227,7 @@ Aplikuje `$callback` na všechny prvky v poli a vrátí pole s vrácenými hodno

/--php
$array = ['foo', 'bar', 'baz'];
$res = Arrays::map($array, function ($value) { return $value . $value; });
$res = Arrays::map($array, function ($value): string { return $value . $value; });
// $res = ['foofoo', 'barbar', 'bazbaz']
\--

@@ -120,7 +120,7 @@ Jinými slovy: vezme se definice funkce `handle{signal}` a všechny parametry, k
Ukázka zpracování signálu:

/--php
public function handleClick($x, $y)
public function handleClick(int $x, int $y)
{
if (!$this->isClickable($x, $y)) {
throw new Nette\Application\UI\BadSignalException('Action not allowed.');
@@ -212,13 +212,13 @@ class PollControl extends Control
/** @var int Id ankety pro kterou vytváříme komponentu */
private $pollId;

public function __construct($pollId, PollManager $pollManager)
public function __construct(int $pollId, PollManager $pollManager)
{
$this->pollManager = $pollManager;
$this->pollId = $pollId;
}

public function handleVote($voteId)
public function handleVote(int $voteId)
{
$this->pollManager->vote($pollId, $voteId);
//...
@@ -239,10 +239,7 @@ class PollControlFactory
$this->pollManager = $pollManager;
}

/**
* @return PollControl
*/
public function create($pollId)
public function create(int $pollId): PollControl
{
return new PollControl($pollId, $this->pollManager);
}
@@ -269,7 +266,7 @@ class PollPresenter extends Nette\UI\Application\Presenter
$this->pollControlFactory = $pollControlFactory;
}

protected function createComponentPollControl()
protected function createComponentPollControl(): PollControl
{
$pollId = 1; // můžeme si předat náš parametr
return $this->pollControlFactory->create($pollId);
@@ -282,10 +279,7 @@ Naštěstí Nette takovéhle jednoduché továrny umí generovat, takže místo
/--php
interface IPollControlFactory
{
/**
* @return PollControl
*/
public function create($pollId);
public function create(int $pollId): PollControl;
}
\--

@@ -142,7 +142,7 @@ class ArticleFactory
...
}

function create()
function create(): Article
{
return new Article($this->database, $this->cacheStorage, ...);
}
@@ -177,12 +177,12 @@ Příkladem může být kontejner, který vytvoří objekt `ArticleFactory`, ale
/--php
class Container
{
function createDatabase()
function createDatabase(): Nette\Database\Connection
{
return new Nette\Database\Connection('mysql:', 'root', '***');
}

function createArticleFactory()
function createArticleFactory(): ArticleFactory
{
return new ArticleFactory($this->createDatabase());
}
@@ -208,7 +208,7 @@ class Container
$this->parameters = $parameters;
}

function createDatabase()
function createDatabase(): Nette\Database\Connection
{
return new Nette\Database\Connection(
$this->parameters['dsn'],
@@ -217,7 +217,7 @@ class Container
);
}

function createArticleFactory()
function createArticleFactory(): ArticleFactory
{
return new ArticleFactory($this->createDatabase());
}
@@ -245,7 +245,7 @@ class Container
$this->parameters = $parameters;
}

function createDatabase()
function createDatabase(): Nette\Database\Connection
{
return new Nette\Database\Connection(
$this->parameters['dsn'],
@@ -254,12 +254,12 @@ class Container
);
}

function createArticleFactory()
function createArticleFactory(): ArticleFactory
{
return new ArticleFactory($this->getService('Database'));
}

function getService($name)
function getService(string $name)
{
if (!isset($this->services[$name])) {
// getService('Database') bude volat createDatabase()
@@ -285,7 +285,7 @@ Názvy továrních funkcí dodržují jednotnou konvenci, jsou tvořeny předpon
/--php
class MyContainer extends Nette\DI\Container
{
function createServiceDatabase()
function createServiceDatabase(): Nette\Database\Connection
{
return new Nette\Database\Connection(
$this->parameters['dsn'],
@@ -294,7 +294,7 @@ class MyContainer extends Nette\DI\Container
);
}

function createServiceArticleFactory()
function createServiceArticleFactory(): ArticleFactory
{
return new ArticleFactory($this->getService('database'));
}
@@ -72,7 +72,7 @@ services:
Což vygeneruje tovární metodu v DI kontejneru:

/--php
function createServiceDatabase()
function createServiceDatabase(): PDO
{
$service = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'secret');
return $service;
@@ -127,15 +127,15 @@ services:
Vygeneruje:

/--php
public function createServiceDatabase()
public function createServiceDatabase(): PDO
{
$service = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'secret');
$service->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$service->mode = 123;
return $service;
}

public function createServiceMyService()
public function createServiceMyService(): MyService
{
$service = new MyService;
MyService::$foo = 2;
@@ -157,7 +157,7 @@ services:
Vygeneruje:

/--php
public function createServiceAnalyser()
public function createServiceAnalyser(): My\Analyser
{
return new My\Analyser(
new FilesystemIterator('...'),
@@ -208,7 +208,7 @@ services:
Vygeneruje:

/--php
public function createServiceArticles()
public function createServiceArticles(): Model\ArticleRepository
{
$service = new Model\ArticleRepository($this->getService('database'));
$service->setCacheStorage($this->getService('cache.storage'));
@@ -242,12 +242,12 @@ services:
Vygeneruje:

/--php
public function createServiceRouterFactory()
public function createServiceRouterFactory(): App\Router\Factory
{
return new App\Router\Factory;
}

public function createServiceRouter()
public function createServiceRouter(): IRouter
{
return $this->getService('routerFactory')->create();
}
@@ -282,7 +282,7 @@ services:
Vygeneruje:

/--php
public function createServiceRouterFactory()
public function createServiceRouterFactory(): App\Router\Factory
{
$service = new App\Router\Factory( Foo::bar() );
$service->setIp( $this->getService('http.request')->getRemoteAddress() );
@@ -304,7 +304,7 @@ services:
Vygeneruje:

/--php
public function createServiceArticles()
public function createServiceArticles(): Model\ArticleRepository
{
$service = new Model\ArticleRepository( new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'secret') );
$service->setCacheStorage( new Nette\Caching\Storages\DevNullStorage );
@@ -323,7 +323,7 @@ services:
Vygeneruje:

/--php
public function createServiceRouter()
public function createServiceRouter(): IRouter
{
return (new App\Router\Factory())->create();
}
@@ -214,7 +214,7 @@ class MyPresenter extends Nette\Application\UI\Presenter
$this->service2 = $service2;
}

protected function createComponentMyControl()
protected function createComponentMyControl(): MyControl
{
$control = new MyControl($this->service1);
$control->setService2($this->service2);
@@ -239,7 +239,7 @@ class MySecondControl extends Nette\Application\UI\Control
// přiřazení do členských proměnných $service1, $service2, $service3
}

protected function createComponentMyControl()
protected function createComponentMyControl(): MyControl
{
$control = new MyControl($this->service1);
$control->setService2($this->service2);
@@ -363,7 +363,7 @@ class ImagePresenter extends Nette\Application\UI\Presenter
private $imageDir;
private $optimizer;

public function __construct($imageDir, ImageOptimizer $optimizer)
public function __construct(string $imageDir, ImageOptimizer $optimizer)
{
$this->imageDir = $imageDir;
$this->optimizer = $optimizer;
@@ -390,10 +390,7 @@ namespace App\Components;

interface IUserTableFactory
{
/**
* @return UserTable
*/
public function create();
public function create(): UserTable;
}
\--

@@ -438,7 +435,7 @@ class UserPresenter extends Nette\Application\UI\Presenter
/** @var \App\Components\IUserTableFactory @inject */
public $userTableFactory;

protected function createComponentUserTable()
protected function createComponentUserTable(): UserTable
{
return $this->userTableFactory->create();
}
@@ -131,7 +131,7 @@ foreach (Finder::findFiles('*.php')->date('>', '- 2 weeks')
Zde se prochází soubory PHP s počtem řádku větším než 1000. Jako filtr použijeme vlastní callback:

/--php
$finder = Finder::findFiles('*.php')->filter(function ($file) {
$finder = Finder::findFiles('*.php')->filter(function (SplFileInfo $file): bool {
return count(file($file->getPathname())) > 1000;
})->from($dir);
\--
@@ -145,7 +145,7 @@ Finder::extensionMethod('dimensions', function($finder, $width, $height){
) {
throw new InvalidArgumentException('Invalid dimensions predicate format.');
}
return $finder->filter(function ($file) use ($mW, $mH) {
return $finder->filter(function ($file) use ($mW, $mH): bool {
return $file->getSize() >= 12 && ($size = getimagesize($file->getPathname()))
&& (!$mW || Finder::compare($size[0], $mW[1], $mW[2]))
&& (!$mH || Finder::compare($size[1], $mH[1], $mH[2]));
Oops, something went wrong.

0 comments on commit c4a7c65

Please sign in to comment.
You can’t perform that action at this time.