for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types = 1);
namespace Puzzle\QueryBuilder\Queries\Snippets;
use Puzzle\QueryBuilder\Snippet;
class Count implements Snippet, Selectable
{
private
$columnName,
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.
$columnName
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
class A { var $property; }
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.
$alias;
/**
* @param Snippet|string $columnName
*/
public function __construct($columnName, ?string $alias = null)
if((! $columnName instanceof Snippet) && empty($columnName))
throw new \InvalidArgumentException('Empty column name.');
}
$this->columnName = $columnName;
$this->alias = $alias;
public function toString(): string
return implode(' ', array_filter(array(
$this->buildCountSnippet(),
$this->buildAliasSnippet()
)));
private function buildCountSnippet(): string
$columnName = $this->columnName;
if($columnName instanceof Snippet)
$columnName = $columnName->toString();
return sprintf('COUNT(%s)', $columnName);
private function buildAliasSnippet(): string
$alias = $this->alias;
if(! empty($alias))
return sprintf('AS %s', $alias);
return '';
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.