Skip to content

Commit

Permalink
Add return type hints (#21)
Browse files Browse the repository at this point in the history
This improves forward compatibility with upcoming Symfony versions.
  • Loading branch information
mpdude authored May 5, 2022
1 parent 4c2560f commit 28f164e
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 81 deletions.
18 changes: 2 additions & 16 deletions src/Config/DoctrineEntityClassResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,13 @@ public function __construct($classname)
$this->classname = $classname;
}

public function register(MetaQuery $query)
public function register(MetaQuery $query): void
{
$query->addEntityClass($this->classname);
}

public function __toString()
public function __toString(): string
{
return self::class.' '.$this->classname;
}

/**
* @deprecated, only present for BC with Symfony 2.x. Remove for Symfony 3.x.
*/
public function isFresh($timestamp)
{
}

/**
* @deprecated, only present for BC with Symfony 2.x. Remove for Symfony 3.x.
*/
public function getResource()
{
}
}
10 changes: 5 additions & 5 deletions src/Config/WfdMetaConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function __construct($file, ConfigCacheInterface $innerCache, MetaQueryFa
$this->metaQueryFactory = $metaQueryFactory;
}

public function getPath()
public function getPath(): string
{
return $this->innerCache->getPath();
}

public function isFresh()
public function isFresh(): bool
{
if (!$this->innerCache->isFresh()) {
return false;
Expand All @@ -48,7 +48,7 @@ public function isFresh()
return true;
}

public function isWfdMetaFresh()
public function isWfdMetaFresh(): bool
{
$wfdMetaFile = $this->file.'.wfd_meta';

Expand All @@ -71,7 +71,7 @@ public function isWfdMetaFresh()
return $metaQuery->getLastTouched() === $wfdMetaResources['timestamp'];
}

public function write($content, array $metadata = null)
public function write($content, array $metadata = null): void
{
/** @var WfdMetaResource[] $wfdMetaResources */
$wfdMetaResources = [];
Expand All @@ -98,7 +98,7 @@ public function write($content, array $metadata = null)
$this->dumpWfdMetaFile(serialize(['resources' => array_values($wfdMetaResources), 'timestamp' => $timestamp]));
}

private function dumpWfdMetaFile($content)
private function dumpWfdMetaFile($content): void
{
$mode = 0666;
$umask = umask();
Expand Down
6 changes: 3 additions & 3 deletions src/Config/WfdMetaConfigCacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(ConfigCacheFactoryInterface $configCacheFactory, Met
$this->lockFactory = $lockFactory;
}

public function cache($file, $callback)
public function cache($file, $callback): ConfigCacheInterface
{
if (!\is_callable($callback)) {
throw new InvalidArgumentException(sprintf('Invalid type for callback argument. Expected callable, but got "%s".', \gettype($callback)));
Expand All @@ -68,12 +68,12 @@ public function cache($file, $callback)
return $wfdMetaCache;
}

private function createCache($file, ConfigCacheInterface $innerCache)
private function createCache($file, ConfigCacheInterface $innerCache): ConfigCacheInterface
{
return new WfdMetaConfigCache($file, $innerCache, $this->metaQueryFactory);
}

private function fillCache($callback, ConfigCacheInterface $cache)
private function fillCache($callback, ConfigCacheInterface $cache): void
{
// Make sure only one process (on this host) will rebuild the cache, others wait for it
$cs = new CriticalSection($this->lockFactory);
Expand Down
3 changes: 3 additions & 0 deletions src/Config/WfdMetaResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
*/
interface WfdMetaResource
{
/**
* @return void
*/
public function register(MetaQuery $query);
}
18 changes: 2 additions & 16 deletions src/Config/WfdTableResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,13 @@ public function __construct($tablename)
$this->tablename = $tablename;
}

public function register(MetaQuery $query)
public function register(MetaQuery $query): void
{
$query->addTable($this->tablename);
}

public function __toString()
public function __toString(): string
{
return self::class.' '.$this->tablename;
}

/**
* @deprecated, only present for BC with Symfony 2.x. Remove for Symfony 3.x.
*/
public function isFresh($timestamp)
{
}

/**
* @deprecated, only present for BC with Symfony 2.x. Remove for Symfony 3.x.
*/
public function getResource()
{
}
}
4 changes: 2 additions & 2 deletions src/Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function templateAction(
$metaTableConstants = null,
$metaEntities = null,
$metaResetInterval = null
) {
): Response {
/** @var DateTime $lastmod */
$lastmod = null;

/** @var $response \Symfony\Component\HttpFoundation\Response */
/** @var $response Response */
$response = null;

if ($metaTables || $metaTableConstants || $metaEntities) {
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/WebfactoryWfdMetaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class WebfactoryWfdMetaExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$fileLocator = new FileLocator(__DIR__.'/../Resources/config');

Expand Down
2 changes: 1 addition & 1 deletion src/DoctrineMetadataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(ClassMetadataFactory $metadataFactory)
*
* @return string Database table name for the root table belonging to this entity.
*/
public function getRootTableName($classname)
public function getRootTableName(string $classname): string
{
try {
/** @var $meta ClassMetadata */
Expand Down
13 changes: 7 additions & 6 deletions src/Helper/LastmodHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Webfactory\Bundle\WfdMetaBundle\Helper;

use DateTime;
use DateTimeInterface;
use Exception;
use RuntimeException;
use Webfactory\Bundle\WfdMetaBundle\MetaQuery;
Expand All @@ -21,7 +22,7 @@ class LastmodHelper
protected $entities;
protected $resetInterval = 2419200; // Default: 28 Tage

public function calculateLastModified(MetaQueryFactory $metaQueryFactory)
public function calculateLastModified(MetaQueryFactory $metaQueryFactory): ?DateTimeInterface
{
$metaQuery = $metaQueryFactory->create();
$this->configure($metaQuery);
Expand All @@ -38,7 +39,7 @@ public function calculateLastModified(MetaQueryFactory $metaQueryFactory)
return null;
}

protected function configure(MetaQuery $metaQuery)
protected function configure(MetaQuery $metaQuery): void
{
try {
if (!$this->tables && !$this->tableIdConstants && !$this->entities) {
Expand Down Expand Up @@ -66,22 +67,22 @@ protected function configure(MetaQuery $metaQuery)
}
}

public function setResetInterval($resetInterval)
public function setResetInterval(int $resetInterval): void
{
$this->resetInterval = $resetInterval;
}

public function setEntities($entities)
public function setEntities(array $entities): void
{
$this->entities = $entities;
}

public function setTableIdConstants($tableIdConstants)
public function setTableIdConstants(array $tableIdConstants): void
{
$this->tableIdConstants = $tableIdConstants;
}

public function setTables($tables)
public function setTables(array $tables): void
{
$this->tables = $tables;
}
Expand Down
23 changes: 10 additions & 13 deletions src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Provider implements ServiceSubscriberInterface
*/
private $container;

public static function getSubscribedServices()
public static function getSubscribedServices(): array
{
return [Connection::class];
}
Expand All @@ -36,10 +36,7 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

/**
* @return Connection
*/
private function getConnection()
private function getConnection(): Connection
{
return $this->container->get(Connection::class);
}
Expand All @@ -49,9 +46,9 @@ private function getConnection()
*
* @param array $tableNamesOrIds The table names or table ids to check for changes.
*
* @return int|null UNIX timestamp or null if no entries were found.
* @return ?int UNIX timestamp or null if no entries were found.
*/
public function getLastTouched(array $tableNamesOrIds)
public function getLastTouched(array $tableNamesOrIds): ?int
{
if (!$tableNamesOrIds) {
return 0;
Expand Down Expand Up @@ -99,7 +96,7 @@ public function getLastTouched(array $tableNamesOrIds)
*
* @return array (int id => int unix timestamp of last change)
*/
public function getLastTouchedOfEachRow($tableName)
public function getLastTouchedOfEachRow($tableName): array
{
$lastTouchedData = $this->getConnection()->fetchAll('
SELECT m.data_id, m.last_touched
Expand All @@ -123,10 +120,10 @@ public function getLastTouchedOfEachRow($tableName)
* @param string $tableNameOrId The table name or ID containing the data row in question
* @param int $primaryKey The data-id of the row in question
*
* @return int|null The Unix timestamp for the last change of the given row; null if the information is not
* available
* @return ?int The Unix timestamp for the last change of the given row; null if the information is not
* available
*/
public function getLastTouchedRow($tableNameOrId, $primaryKey)
public function getLastTouchedRow($tableNameOrId, $primaryKey): ?int
{
$lastTouched = $this->getConnection()->fetchColumn('
SELECT m.last_touched
Expand All @@ -143,9 +140,9 @@ public function getLastTouchedRow($tableNameOrId, $primaryKey)
* @param string|bool|null $fetchValue string in "YYYY-m-d H:i:s" format or some "not queryable" value like NULL
* or false
*
* @return int|null UNIX timestamp or NULL
* @return ?int UNIX timestamp or NULL
*/
private function getTimestampOrNull($fetchValue)
private function getTimestampOrNull($fetchValue): ?int
{
if (false === $fetchValue || null === $fetchValue) {
return null;
Expand Down
21 changes: 5 additions & 16 deletions src/Util/CriticalSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RuntimeException;
use Symfony\Component\Lock\Lock;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\LockInterface;

/**
* Make sure that code - provided as a closure - is being run sequentially by different
Expand Down Expand Up @@ -87,10 +88,7 @@ public function execute($file, Closure $callback)
}
}

/**
* @param string $lockName
*/
private function lock($lockName)
private function lock(string $lockName)
{
$this->debug("Requesting lock $lockName");
if (!$this->getLock($lockName)->acquire(true)) {
Expand All @@ -104,10 +102,7 @@ private function lock($lockName)
$this->debug("Obtained the lock $lockName");
}

/**
* @param string $lockName
*/
private function release($lockName)
private function release(string $lockName)
{
--self::$entranceCount[$lockName];
if (0 === self::$entranceCount[$lockName]) {
Expand All @@ -121,12 +116,8 @@ private function release($lockName)
*
* A new lock object will be created if it does not exist yet.
* This method will *not* automatically acquire the lock.
*
* @param string $name
*
* @return Lock
*/
private function getLock($name)
private function getLock(string $name): LockInterface
{
if (!isset(self::$locks[$name])) {
$lock = $this->lockFactory->createLock($name);
Expand All @@ -138,10 +129,8 @@ private function getLock($name)

/**
* Logs the given message if a logger is available.
*
* @param string $message
*/
private function debug($message)
private function debug(string $message): void
{
if ($this->logger) {
$this->logger->debug($message, ['pid' => getmypid()]);
Expand Down
4 changes: 2 additions & 2 deletions src/Util/ExpirableConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct($baseFilename, $debug, $timestamp)
parent::__construct($this->timestampedFile, $debug);
}

public function write($content, array $metadata = null)
public function write(string $content, array $metadata = null): void
{
parent::write($content, $metadata);

Expand All @@ -54,7 +54,7 @@ public function write($content, array $metadata = null)
$this->cleanup();
}

protected function cleanup()
protected function cleanup(): void
{
$finder = new Finder();
$basename = basename($this->baseFilename);
Expand Down

0 comments on commit 28f164e

Please sign in to comment.