SlideShare a Scribd company logo
Dando Nome aos Códigos
@nelson_senna
Por que nomes
importam?
“Existem duas coisas difíceis em
Ciência da Computação:
invalidação de cache e nomear
coisas.”
— Phil Karlton
Por que é difícil?
“palavra ou locução com que se
designa uma classe de coisas,
pessoas, animais, um lugar, um
acidente geográfico, um astro
etc.;”
Nome
De onde eu tiro bons
nomes?
Nomes são
descobertos!
O processo de descoberta
Sem nome Sem sentido Honesto
Honesto e
Completo
Faz o que o
nome diz
Intenção
Abstração de
domínio
↪ ↪ ↪ ↪ ↪ ↪
O processo de descoberta
Sem nome Sem sentido Honesto
Honesto e
Completo
Faz o que o
nome diz
Intenção
Abstração de
domínio
↪ ↪ ↪ ↪ ↪ ↪
@arlobelshee
O processo de descoberta
Sem nome Sem sentido Honesto
Honesto e
Completo
Faz o que o
nome diz
Intenção
Abstração de
domínio
↪ ↪ ↪ ↪ ↪ ↪
@arlobelshee
Vamos até aqui!
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
Nosso código
↳
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
Assinatura
↳
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
Parâmetros booleanos…
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
Um loop com if, eh?
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
Sem nome!
private function getLists($iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
Nome sem sentido
↳
private function getLists($iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
Hmmm, um filtro!
private function getLists($iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
Cria uma lista de pastas e,

outra lista de arquivos
private function iterateOverItemsToCreateSortableMapOfFilesAndDirs(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
Honesto!
private function iterateOverItemsToCreateSortableMapOfFilesAndDirs(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
Hmmm, um filtro!
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
Honesto e completo!
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
Olha o filtro aí!
Organizando as
ideias!
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
Relação!
Nomeando métodos
Características de bons nomes de métodos
•Use verbos;
•Sem abreviações;
•Conciso (mas, não muito!);
•Use perguntas quando o método retornar um boolean (isEmpty(), canDo());
•Não use “And” e “Or” nos seus nomes;
•Não deixe o nome redundante com o argumento

($list->addItem($item));
•Não deixe o nome redundante com quem chama

($list->addToList($item));
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
Guard Clauses
if ($item->isDot()) {
continue;
}
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
private function isFileRelevant($item, $skipHidden, $ignoredFiles)
{
if ($item->isDot()) {
return false;
}
$name = $item->getFilename();
$isHiddenFile = $name[0] === '.';
$ignored = isset($ignoredFiles[$name]);
if ($skipHidden && $isHiddenFile || $ignored) {
return false;
}
return true;
}
Bom nome?
•Use verbos √
•Sem abreviações √
•Conciso (mas, não muito!) √
•Use perguntas quando o método retornar um boolean (isEmpty(), canDo()) √
•Não use “And” e “Or” nos seus nomes √
•Não deixe o nome redundante com o argumento √

($list->addItem($item));
•Não deixe o nome redundante com quem chama √

($list->addToList($item));
private function isFileRelevant($item, $skipHidden, $ignoredFiles)
{
if ($item->isDot()) {
return false;
}
$name = $item->getFilename();
$isHiddenFile = $name[0] === '.';
$ignored = isset($ignoredFiles[$name]);
if ($skipHidden && $isHiddenFile || $ignored) {
return false;
}
return true;
}
private function isFileRelevant($item, $skipHidden, $ignoredFiles)
{
if ($item->isDot()) {
return false;
}
$name = $item->getFilename();
$isHiddenFile = $name[0] === '.';
$ignored = isset($ignoredFiles[$name]);
if ($skipHidden && $isHiddenFile || $ignored) {
return false;
}
return true;
}
private function isFileRelevant($item, $skipHidden, $ignoredFiles)
{
if ($item->isDot()) {
return false;
}
$name = $item->getFilename();
$isHiddenFile = $name[0] === '.';
$ignored = isset($ignoredFiles[$name]);
if ($skipHidden && $isHiddenFile || $ignored) {
return false;
}
return true;
}
private function isFileRelevant($item, $skipHidden, $ignoredFiles)
{
if ($item->isDot()) {
return false;
}
$name = $item->getFilename();
$isHiddenFile = $name[0] === '.';
$ignored = isset($ignoredFiles[$name]);
if ($skipHidden && $isHiddenFile || $ignored) {
return false;
}
return true;
}
private function isFileRelevant($item, $skipHidden, $ignoredFiles)
{
if ($item->isDot()) {
return false;
}
$name = $item->getFilename();
$isHiddenFile = $name[0] === '.';
$ignored = isset($ignoredFiles[$name]);
if ($skipHidden && $isHiddenFile || $ignored) {
return false;
}
return true;
}
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($this->isFileRelevant($item, $skipHidden, $exceptions)) {
$name = $fullPath ? $item->getPathname() : $item->getFilename();
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
}
return [$dirs, $files];
}
private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions(
$iterator, $methodName, $exceptions, $skipHidden, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
if ($this->isFileRelevant($item, $skipHidden, $exceptions)) {
$name = $fullPath ? $item->getPathname() : $item->getFilename();
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
}
return [$dirs, $files];
}
Extrair comportamento pra

sua própria classe
Características de bons nomes de classes
•Use substantivos e adjetivos*;
•Não use muitos adjetivos (AbstractFactoryPatternBase);
•Não use “Manager”, “Helper” e “Data” nos seus nomes;
•Não use Sufixos do seu namespace (Service, Factory,
Iterator)
•Evite usar “er”, “or”, “tion”, “sion” (ObjectFinder,
DataProcessor, Conversion, DataManipulation)
–Chris Oldwood
“So, a "DateTimeProvider", that's basically just a clock, right?”
Características de bons nomes de classes
Service is the new Enzo
private function isFileRelevant($item, $skipHidden, $ignoredFiles)
{
if ($item->isDot()) {
return false;
}
$name = $item->getFilename();
$isHiddenFile = $name[0] === '.';
$ignored = isset($ignoredFiles[$name]);
if ($skipHidden && $isHiddenFile || $ignored) {
return false;
}
return true;
}
Filtro para arquivos

ocultos
private function isFileRelevant($item, $skipHidden, $ignoredFiles)
{
if ($item->isDot()) {
return false;
}
$name = $item->getFilename();
$isHiddenFile = $name[0] === '.';
$ignored = isset($ignoredFiles[$name]);
if ($skipHidden && $isHiddenFile || $ignored) {
return false;
}
return true;
}
Filtro para arquivos

ignorados
namespace Filter;
class Visible extends FilterIterator
{
public function accept()
{
$item = $this->getInnerIterator()->current();
$name = $item->getFilename();
$isDotEntry = $name[0] === '.';
return !$isDotEntry;
}
}
namespace Filter;
class IgnoreList extends FilterIterator
{
private $list = [];
public function __construct(Iterator $iterator, $list)
{
parent::__construct($iterator);
$this->list = $list;
}
public function accept()
{
if ($item->isDot()) {
return false;
}
$item = $this->getInnerIterator()->current();
$relevant = !isset($this->list[$item->getFilename()]);
return $relevant;
}
}
private function applyContentFilters(Iterator $iterator, $ignoreList, $skipHidden)
{
if ($skipHidden) {
$iterator = new FilterVisible($iterator);
}
return new FilterIgnoreList($iterator, $ignoreList);
}
private function createSortableMapOfFilesAndDirs($iterator, $methodName, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
$name = $fullPath ? $item->getPathname() : $item->getFilename();
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
Iterator já filtrado!
Próximos passos…
private function createSortableMapOfFilesAndDirs($iterator, $methodName, $fullPath)
{
$dirs = $files = [];
foreach ($iterator as $item) {
$name = $fullPath ? $item->getPathname() : $item->getFilename();
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
return [$dirs, $files];
}
Remover a ordenação

daqui!
No método
principal, caçar o
que ainda não tem
nome!
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
Implementação

“rústica” de Set
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
Nome
“nonsense”
↳
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
Nome
“nonsense”
↳
Configuração em runtime?

Builder Pattern!
Algumas mudanças
depois…
DirectoryContents::of($path)
->excluding($irrelevantFiles)
->orderedByName() // ou orderedByLastModification
->includeHiddenContent()
->list(); // ou listWithFullPath()
DirectoryContents::of($path)
->excluding($irrelevantFiles)
->orderedByName() // ou orderedByLastModification
->includeHiddenContent()
->list(); // ou listWithFullPath()
Informação essencial

pra construir a classe
DirectoryContents::of($path)
->excluding($irrelevantFiles)
->orderedByName() // ou orderedByLastModification
->includeHiddenContent()
->list(); // ou listWithFullPath()
Exceções
DirectoryContents::of($path)
->excluding($irrelevantFiles)
->orderedByName() // ou orderedByLastModification
->includeHiddenContent()
->list(); // ou listWithFullPath()
Ordenação
DirectoryContents::of($path)
->excluding($irrelevantFiles)
->orderedByName() // ou orderedByLastModification
->excludeHiddenContent()
->list(); // ou listWithFullPath()
Incluir ou não arquivos ocultos
DirectoryContents::of($path)
->excluding($irrelevantFiles)
->orderedByName() // ou orderedByLastModification
->includeHiddenContent()
->list(); // ou listWithFullPath()
O resultado após as

configurações serem

aplicadas
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
<?php
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
{
$dirs = $files = [];
if (!$this->pwd()) {
return [$dirs, $files];
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = isset($exceptions['.']) || $exceptions === true;
try {
$iterator = new DirectoryIterator($this->path);
} catch (Exception $e) {
return [$dirs, $files];
}
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}
$name = $item->getFilename();
if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) {
continue;
}
if ($fullPath) {
$name = $item->getPathname();
}
if ($item->isDir()) {
$dirs[$item->{$methodName}()][] = $name;
} else {
$files[$item->{$methodName}()][] = $name;
}
}
if ($sort || $this->sort) {
ksort($dirs);
ksort($files);
}
if ($dirs) {
$dirs = array_merge(...array_values($dirs));
}
if ($files) {
$files = array_merge(...array_values($files));
}
return [$dirs, $files];
}
/**
* Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files.
*
* @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path
* @return array Contents of current directory as an array, an empty array on failure
*/
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
Concluindo…
1. Procure por lugares com mais de um nível de indentação
2. Extraia e dê nome para o que fizer sentido
3. Descreva o que o método realmente faz, sem vergonha, seja honesto!
4. Vá diminuindo o nome do método extraindo suas responsabilidades
5. Excesso de métodos privados para um dado contexto: Uma nova classe!
6. Procure na documentação ou fale com um amiguinho para descrever

o problema de domínio resolvido
7. Seja feliz!
Obrigado!
Referências
•http://arlobelshee.com/good-naming-is-a-process-not-a-single-
step/
•http://journal.stuffwithstuff.com/2009/06/05/naming-things-
in-code/
•https://martinfowler.com/bliki/TwoHardThings.html
•https://www.infoq.com/articles/implementations-patterns-br

More Related Content

What's hot

Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tablesMind The Firebird
 
Sqlite perl
Sqlite perlSqlite perl
Sqlite perl
Ashoka Vanjare
 
Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perl
sana mateen
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Workhorse Computing
 
Save Repository From Save
Save Repository From SaveSave Repository From Save
Save Repository From Save
Norbert Orzechowicz
 
Perl
PerlPerl
Swift tips and tricks
Swift tips and tricksSwift tips and tricks
Swift tips and tricks
HomeroJuniorOliveira1
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
Mark Baker
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
José Lorenzo Rodríguez Urdaneta
 
DHWI Linked Open Data - What I Did
DHWI Linked Open Data - What I DidDHWI Linked Open Data - What I Did
DHWI Linked Open Data - What I Did
Georgina Goodlander
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arraysKumar
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
Marc Logghe
 
Working with Groovy Collections
Working with Groovy CollectionsWorking with Groovy Collections
Working with Groovy Collections
Ted Vinke
 
Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2
Marco Gralike
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
Valentine Dianov
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3
markstory
 

What's hot (18)

Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tables
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Sqlite perl
Sqlite perlSqlite perl
Sqlite perl
 
Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perl
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
 
Save Repository From Save
Save Repository From SaveSave Repository From Save
Save Repository From Save
 
Perl
PerlPerl
Perl
 
Swift tips and tricks
Swift tips and tricksSwift tips and tricks
Swift tips and tricks
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
DHWI Linked Open Data - What I Did
DHWI Linked Open Data - What I DidDHWI Linked Open Data - What I Did
DHWI Linked Open Data - What I Did
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
 
Working with Groovy Collections
Working with Groovy CollectionsWorking with Groovy Collections
Working with Groovy Collections
 
Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2Starting with JSON Path Expressions in Oracle 12.1.0.2
Starting with JSON Path Expressions in Oracle 12.1.0.2
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3
 

Similar to Dando nome aos códigos

Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
Chris Tankersley
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
Nate Abele
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
Workhorse Computing
 
Array ============ 1. In array, elements can be accessed using .pdf
Array ============ 1. In array, elements can be accessed using .pdfArray ============ 1. In array, elements can be accessed using .pdf
Array ============ 1. In array, elements can be accessed using .pdf
anjaliselectionahd
 
Yy
YyYy
Yyyygh
 
Yy
YyYy
Yyyygh
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texteSai Ef
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
Nikita Popov
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Oops in php
Oops in phpOops in php
PHP Tips & Tricks
PHP Tips & TricksPHP Tips & Tricks
PHP Tips & Tricks
Radek Benkel
 
DRUPAL 8 STORAGES OVERVIEW
DRUPAL 8 STORAGES OVERVIEWDRUPAL 8 STORAGES OVERVIEW
DRUPAL 8 STORAGES OVERVIEW
DrupalCamp Kyiv
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
arihantmobileselepun
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 

Similar to Dando nome aos códigos (20)

Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
C99
C99C99
C99
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
Array ============ 1. In array, elements can be accessed using .pdf
Array ============ 1. In array, elements can be accessed using .pdfArray ============ 1. In array, elements can be accessed using .pdf
Array ============ 1. In array, elements can be accessed using .pdf
 
Yy
YyYy
Yy
 
Yy
YyYy
Yy
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texte
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
zinno
zinnozinno
zinno
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Oops in php
Oops in phpOops in php
Oops in php
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
PHP Tips & Tricks
PHP Tips & TricksPHP Tips & Tricks
PHP Tips & Tricks
 
DRUPAL 8 STORAGES OVERVIEW
DRUPAL 8 STORAGES OVERVIEWDRUPAL 8 STORAGES OVERVIEW
DRUPAL 8 STORAGES OVERVIEW
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 

More from Nelson Senna do Amaral

Veni vedi vici.
Veni vedi vici.Veni vedi vici.
Veni vedi vici.
Nelson Senna do Amaral
 
Pague o aluguel
Pague o aluguelPague o aluguel
Pague o aluguel
Nelson Senna do Amaral
 
Melhorando seu código com Law of Demeter e Tell don't ask
Melhorando seu código com Law of Demeter e Tell don't askMelhorando seu código com Law of Demeter e Tell don't ask
Melhorando seu código com Law of Demeter e Tell don't ask
Nelson Senna do Amaral
 
OOP: Princípios e Padroes
OOP: Princípios e PadroesOOP: Princípios e Padroes
OOP: Princípios e Padroes
Nelson Senna do Amaral
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Domínio: Dividir e conquistar
Domínio: Dividir e conquistarDomínio: Dividir e conquistar
Domínio: Dividir e conquistar
Nelson Senna do Amaral
 
Interfaces - Como os objetos deveriam se comportar
Interfaces - Como os objetos deveriam se comportarInterfaces - Como os objetos deveriam se comportar
Interfaces - Como os objetos deveriam se comportar
Nelson Senna do Amaral
 
Nossa experiência com TDD
Nossa experiência com TDDNossa experiência com TDD
Nossa experiência com TDD
Nelson Senna do Amaral
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Nelson Senna do Amaral
 
Tirando o coelho da cartola: integrando sistemas com RabbitMQ
Tirando o coelho da cartola: integrando sistemas com RabbitMQTirando o coelho da cartola: integrando sistemas com RabbitMQ
Tirando o coelho da cartola: integrando sistemas com RabbitMQ
Nelson Senna do Amaral
 

More from Nelson Senna do Amaral (10)

Veni vedi vici.
Veni vedi vici.Veni vedi vici.
Veni vedi vici.
 
Pague o aluguel
Pague o aluguelPague o aluguel
Pague o aluguel
 
Melhorando seu código com Law of Demeter e Tell don't ask
Melhorando seu código com Law of Demeter e Tell don't askMelhorando seu código com Law of Demeter e Tell don't ask
Melhorando seu código com Law of Demeter e Tell don't ask
 
OOP: Princípios e Padroes
OOP: Princípios e PadroesOOP: Princípios e Padroes
OOP: Princípios e Padroes
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
Domínio: Dividir e conquistar
Domínio: Dividir e conquistarDomínio: Dividir e conquistar
Domínio: Dividir e conquistar
 
Interfaces - Como os objetos deveriam se comportar
Interfaces - Como os objetos deveriam se comportarInterfaces - Como os objetos deveriam se comportar
Interfaces - Como os objetos deveriam se comportar
 
Nossa experiência com TDD
Nossa experiência com TDDNossa experiência com TDD
Nossa experiência com TDD
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Tirando o coelho da cartola: integrando sistemas com RabbitMQ
Tirando o coelho da cartola: integrando sistemas com RabbitMQTirando o coelho da cartola: integrando sistemas com RabbitMQ
Tirando o coelho da cartola: integrando sistemas com RabbitMQ
 

Recently uploaded

Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 

Recently uploaded (20)

Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 

Dando nome aos códigos

  • 1. Dando Nome aos Códigos @nelson_senna
  • 3. “Existem duas coisas difíceis em Ciência da Computação: invalidação de cache e nomear coisas.” — Phil Karlton
  • 4. Por que é difícil?
  • 5. “palavra ou locução com que se designa uma classe de coisas, pessoas, animais, um lugar, um acidente geográfico, um astro etc.;” Nome
  • 6. De onde eu tiro bons nomes?
  • 8. O processo de descoberta Sem nome Sem sentido Honesto Honesto e Completo Faz o que o nome diz Intenção Abstração de domínio ↪ ↪ ↪ ↪ ↪ ↪
  • 9. O processo de descoberta Sem nome Sem sentido Honesto Honesto e Completo Faz o que o nome diz Intenção Abstração de domínio ↪ ↪ ↪ ↪ ↪ ↪ @arlobelshee
  • 10. O processo de descoberta Sem nome Sem sentido Honesto Honesto e Completo Faz o que o nome diz Intenção Abstração de domínio ↪ ↪ ↪ ↪ ↪ ↪ @arlobelshee Vamos até aqui!
  • 11. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } Nosso código ↳
  • 12. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) Assinatura ↳
  • 13. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) Parâmetros booleanos…
  • 14. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
  • 15. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } Um loop com if, eh?
  • 16. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } Sem nome!
  • 17. private function getLists($iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } Nome sem sentido ↳
  • 18. private function getLists($iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } Hmmm, um filtro!
  • 19. private function getLists($iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } Cria uma lista de pastas e,
 outra lista de arquivos
  • 20. private function iterateOverItemsToCreateSortableMapOfFilesAndDirs( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } Honesto!
  • 21. private function iterateOverItemsToCreateSortableMapOfFilesAndDirs( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } Hmmm, um filtro!
  • 22. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } Honesto e completo!
  • 23. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } Olha o filtro aí!
  • 25. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; }
  • 26. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; }
  • 27. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; }
  • 28. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } Relação!
  • 30. Características de bons nomes de métodos •Use verbos; •Sem abreviações; •Conciso (mas, não muito!); •Use perguntas quando o método retornar um boolean (isEmpty(), canDo()); •Não use “And” e “Or” nos seus nomes; •Não deixe o nome redundante com o argumento
 ($list->addItem($item)); •Não deixe o nome redundante com quem chama
 ($list->addToList($item));
  • 31. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } Guard Clauses if ($item->isDot()) { continue; } if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; }
  • 32. private function isFileRelevant($item, $skipHidden, $ignoredFiles) { if ($item->isDot()) { return false; } $name = $item->getFilename(); $isHiddenFile = $name[0] === '.'; $ignored = isset($ignoredFiles[$name]); if ($skipHidden && $isHiddenFile || $ignored) { return false; } return true; }
  • 33. Bom nome? •Use verbos √ •Sem abreviações √ •Conciso (mas, não muito!) √ •Use perguntas quando o método retornar um boolean (isEmpty(), canDo()) √ •Não use “And” e “Or” nos seus nomes √ •Não deixe o nome redundante com o argumento √
 ($list->addItem($item)); •Não deixe o nome redundante com quem chama √
 ($list->addToList($item));
  • 34. private function isFileRelevant($item, $skipHidden, $ignoredFiles) { if ($item->isDot()) { return false; } $name = $item->getFilename(); $isHiddenFile = $name[0] === '.'; $ignored = isset($ignoredFiles[$name]); if ($skipHidden && $isHiddenFile || $ignored) { return false; } return true; }
  • 35. private function isFileRelevant($item, $skipHidden, $ignoredFiles) { if ($item->isDot()) { return false; } $name = $item->getFilename(); $isHiddenFile = $name[0] === '.'; $ignored = isset($ignoredFiles[$name]); if ($skipHidden && $isHiddenFile || $ignored) { return false; } return true; }
  • 36. private function isFileRelevant($item, $skipHidden, $ignoredFiles) { if ($item->isDot()) { return false; } $name = $item->getFilename(); $isHiddenFile = $name[0] === '.'; $ignored = isset($ignoredFiles[$name]); if ($skipHidden && $isHiddenFile || $ignored) { return false; } return true; }
  • 37. private function isFileRelevant($item, $skipHidden, $ignoredFiles) { if ($item->isDot()) { return false; } $name = $item->getFilename(); $isHiddenFile = $name[0] === '.'; $ignored = isset($ignoredFiles[$name]); if ($skipHidden && $isHiddenFile || $ignored) { return false; } return true; }
  • 38. private function isFileRelevant($item, $skipHidden, $ignoredFiles) { if ($item->isDot()) { return false; } $name = $item->getFilename(); $isHiddenFile = $name[0] === '.'; $ignored = isset($ignoredFiles[$name]); if ($skipHidden && $isHiddenFile || $ignored) { return false; } return true; }
  • 39. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($this->isFileRelevant($item, $skipHidden, $exceptions)) { $name = $fullPath ? $item->getPathname() : $item->getFilename(); if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } } return [$dirs, $files]; }
  • 40. private function iterateOverItemsToCreateSortableMapOfFilesAndDirsSkippingExceptions( $iterator, $methodName, $exceptions, $skipHidden, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { if ($this->isFileRelevant($item, $skipHidden, $exceptions)) { $name = $fullPath ? $item->getPathname() : $item->getFilename(); if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } } return [$dirs, $files]; } Extrair comportamento pra
 sua própria classe
  • 41. Características de bons nomes de classes •Use substantivos e adjetivos*; •Não use muitos adjetivos (AbstractFactoryPatternBase); •Não use “Manager”, “Helper” e “Data” nos seus nomes; •Não use Sufixos do seu namespace (Service, Factory, Iterator) •Evite usar “er”, “or”, “tion”, “sion” (ObjectFinder, DataProcessor, Conversion, DataManipulation)
  • 42. –Chris Oldwood “So, a "DateTimeProvider", that's basically just a clock, right?”
  • 43. Características de bons nomes de classes Service is the new Enzo
  • 44. private function isFileRelevant($item, $skipHidden, $ignoredFiles) { if ($item->isDot()) { return false; } $name = $item->getFilename(); $isHiddenFile = $name[0] === '.'; $ignored = isset($ignoredFiles[$name]); if ($skipHidden && $isHiddenFile || $ignored) { return false; } return true; } Filtro para arquivos
 ocultos
  • 45. private function isFileRelevant($item, $skipHidden, $ignoredFiles) { if ($item->isDot()) { return false; } $name = $item->getFilename(); $isHiddenFile = $name[0] === '.'; $ignored = isset($ignoredFiles[$name]); if ($skipHidden && $isHiddenFile || $ignored) { return false; } return true; } Filtro para arquivos
 ignorados
  • 46. namespace Filter; class Visible extends FilterIterator { public function accept() { $item = $this->getInnerIterator()->current(); $name = $item->getFilename(); $isDotEntry = $name[0] === '.'; return !$isDotEntry; } }
  • 47. namespace Filter; class IgnoreList extends FilterIterator { private $list = []; public function __construct(Iterator $iterator, $list) { parent::__construct($iterator); $this->list = $list; } public function accept() { if ($item->isDot()) { return false; } $item = $this->getInnerIterator()->current(); $relevant = !isset($this->list[$item->getFilename()]); return $relevant; } }
  • 48. private function applyContentFilters(Iterator $iterator, $ignoreList, $skipHidden) { if ($skipHidden) { $iterator = new FilterVisible($iterator); } return new FilterIgnoreList($iterator, $ignoreList); }
  • 49. private function createSortableMapOfFilesAndDirs($iterator, $methodName, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { $name = $fullPath ? $item->getPathname() : $item->getFilename(); if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } Iterator já filtrado!
  • 51. private function createSortableMapOfFilesAndDirs($iterator, $methodName, $fullPath) { $dirs = $files = []; foreach ($iterator as $item) { $name = $fullPath ? $item->getPathname() : $item->getFilename(); if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } return [$dirs, $files]; } Remover a ordenação
 daqui!
  • 52. No método principal, caçar o que ainda não tem nome!
  • 53. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } Implementação
 “rústica” de Set
  • 54. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) Nome “nonsense” ↳
  • 55. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) Nome “nonsense” ↳ Configuração em runtime?
 Builder Pattern!
  • 57. DirectoryContents::of($path) ->excluding($irrelevantFiles) ->orderedByName() // ou orderedByLastModification ->includeHiddenContent() ->list(); // ou listWithFullPath()
  • 58. DirectoryContents::of($path) ->excluding($irrelevantFiles) ->orderedByName() // ou orderedByLastModification ->includeHiddenContent() ->list(); // ou listWithFullPath() Informação essencial
 pra construir a classe
  • 59. DirectoryContents::of($path) ->excluding($irrelevantFiles) ->orderedByName() // ou orderedByLastModification ->includeHiddenContent() ->list(); // ou listWithFullPath() Exceções
  • 60. DirectoryContents::of($path) ->excluding($irrelevantFiles) ->orderedByName() // ou orderedByLastModification ->includeHiddenContent() ->list(); // ou listWithFullPath() Ordenação
  • 61. DirectoryContents::of($path) ->excluding($irrelevantFiles) ->orderedByName() // ou orderedByLastModification ->excludeHiddenContent() ->list(); // ou listWithFullPath() Incluir ou não arquivos ocultos
  • 62. DirectoryContents::of($path) ->excluding($irrelevantFiles) ->orderedByName() // ou orderedByLastModification ->includeHiddenContent() ->list(); // ou listWithFullPath() O resultado após as
 configurações serem
 aplicadas
  • 63. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
  • 64. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
  • 65. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
  • 66. <?php /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) { $dirs = $files = []; if (!$this->pwd()) { return [$dirs, $files]; } if (is_array($exceptions)) { $exceptions = array_flip($exceptions); } $skipHidden = isset($exceptions['.']) || $exceptions === true; try { $iterator = new DirectoryIterator($this->path); } catch (Exception $e) { return [$dirs, $files]; } if (!is_bool($sort) && isset($this->_fsorts[$sort])) { $methodName = $this->_fsorts[$sort]; } else { $methodName = $this->_fsorts[self::SORT_NAME]; } foreach ($iterator as $item) { if ($item->isDot()) { continue; } $name = $item->getFilename(); if ($skipHidden && $name[0] === '.' || isset($exceptions[$name])) { continue; } if ($fullPath) { $name = $item->getPathname(); } if ($item->isDir()) { $dirs[$item->{$methodName}()][] = $name; } else { $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { ksort($dirs); ksort($files); } if ($dirs) { $dirs = array_merge(...array_values($dirs)); } if ($files) { $files = array_merge(...array_values($files)); } return [$dirs, $files]; } /** * Returns an array of the contents of the current directory. * The returned array holds two arrays: One of directories and one of files. * * @param string|bool $sort Whether you want the results sorted, set this and the sort property * to false to get unsorted results. * @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param bool $fullPath True returns the full path * @return array Contents of current directory as an array, an empty array on failure */ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false)
  • 67. Concluindo… 1. Procure por lugares com mais de um nível de indentação 2. Extraia e dê nome para o que fizer sentido 3. Descreva o que o método realmente faz, sem vergonha, seja honesto! 4. Vá diminuindo o nome do método extraindo suas responsabilidades 5. Excesso de métodos privados para um dado contexto: Uma nova classe! 6. Procure na documentação ou fale com um amiguinho para descrever
 o problema de domínio resolvido 7. Seja feliz!