Skip to content

Commit

Permalink
Removed data type validation for storing and added param and return t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
fire015 committed Mar 12, 2019
1 parent 2371d63 commit 4cd00b2
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 173 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
==========

### 12/03/2019 - 2.2
* Bump minimum PHP version to 7.0
* Update PHPUnit to version 6
* Removed data type validation for storing
* Added param and return types

### 09/06/2017 - 2.1.1
* Update `Database::writeTempToFile` to correctly close the file pointer and free up memory

Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
>
<filter>
Expand Down
9 changes: 0 additions & 9 deletions src/Cache/ArrayCache.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Flintstone package.
*
* (c) Jason M <emailfire@gmail.com>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Flintstone\Cache;

class ArrayCache implements CacheInterface
Expand Down
9 changes: 0 additions & 9 deletions src/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Flintstone package.
*
* (c) Jason M <emailfire@gmail.com>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Flintstone\Cache;

interface CacheInterface
Expand Down
37 changes: 14 additions & 23 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Flintstone package.
*
* (c) Jason M <emailfire@gmail.com>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Flintstone;

use Flintstone\Cache\ArrayCache;
Expand Down Expand Up @@ -48,15 +39,15 @@ public function __construct(array $config = [])
*
* @return array
*/
protected function normalizeConfig(array $config)
protected function normalizeConfig(array $config): array
{
$defaultConfig = [
'dir' => getcwd(),
'ext' => '.dat',
'gzip' => false,
'cache' => true,
'formatter' => null,
'swap_memory_limit' => 2097152, // 2MB
'swap_memory_limit' => 2097152, // 2MB
];

return array_replace($defaultConfig, $config);
Expand All @@ -67,7 +58,7 @@ protected function normalizeConfig(array $config)
*
* @return string
*/
public function getDir()
public function getDir(): string
{
return $this->config['dir'];
}
Expand All @@ -79,7 +70,7 @@ public function getDir()
*
* @throws Exception
*/
public function setDir($dir)
public function setDir(string $dir)
{
if (!is_dir($dir)) {
throw new Exception('Directory does not exist: ' . $dir);
Expand All @@ -93,7 +84,7 @@ public function setDir($dir)
*
* @return string
*/
public function getExt()
public function getExt(): string
{
if ($this->useGzip()) {
return $this->config['ext'] . '.gz';
Expand All @@ -107,9 +98,9 @@ public function getExt()
*
* @param string $ext
*/
public function setExt($ext)
public function setExt(string $ext)
{
if ('.' != substr($ext, 0, 1)) {
if (substr($ext, 0, 1) !== '.') {
$ext = '.' . $ext;
}

Expand All @@ -121,7 +112,7 @@ public function setExt($ext)
*
* @return bool
*/
public function useGzip()
public function useGzip(): bool
{
return $this->config['gzip'];
}
Expand All @@ -131,9 +122,9 @@ public function useGzip()
*
* @param bool $gzip
*/
public function setGzip($gzip)
public function setGzip(bool $gzip)
{
$this->config['gzip'] = (bool)$gzip;
$this->config['gzip'] = $gzip;
}

/**
Expand Down Expand Up @@ -171,7 +162,7 @@ public function setCache($cache)
*
* @return FormatterInterface
*/
public function getFormatter()
public function getFormatter(): FormatterInterface
{
return $this->config['formatter'];
}
Expand Down Expand Up @@ -201,7 +192,7 @@ public function setFormatter($formatter)
*
* @return int
*/
public function getSwapMemoryLimit()
public function getSwapMemoryLimit(): int
{
return $this->config['swap_memory_limit'];
}
Expand All @@ -211,8 +202,8 @@ public function getSwapMemoryLimit()
*
* @param int $limit
*/
public function setSwapMemoryLimit($limit)
public function setSwapMemoryLimit(int $limit)
{
$this->config['swap_memory_limit'] = (int)$limit;
$this->config['swap_memory_limit'] = $limit;
}
}
30 changes: 11 additions & 19 deletions src/Database.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Flintstone package.
*
* (c) Jason M <emailfire@gmail.com>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Flintstone;

use SplFileObject;
Expand Down Expand Up @@ -77,7 +68,7 @@ class Database
* @param string $name
* @param Config|null $config
*/
public function __construct($name, Config $config = null)
public function __construct(string $name, Config $config = null)
{
$this->setName($name);

Expand All @@ -91,7 +82,7 @@ public function __construct($name, Config $config = null)
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
Expand All @@ -103,7 +94,7 @@ public function getName()
*
* @throws Exception
*/
public function setName($name)
public function setName(string $name)
{
Validation::validateDatabaseName($name);
$this->name = $name;
Expand All @@ -114,7 +105,7 @@ public function setName($name)
*
* @return Config
*/
public function getConfig()
public function getConfig(): Config
{
return $this->config;
}
Expand All @@ -134,7 +125,7 @@ public function setConfig(Config $config)
*
* @return string
*/
public function getPath()
public function getPath(): string
{
return $this->config->getDir() . $this->getName() . $this->config->getExt();
}
Expand All @@ -148,7 +139,7 @@ public function getPath()
*
* @return SplFileObject
*/
protected function openFile($mode)
protected function openFile(int $mode): SplFileObject
{
$path = $this->getPath();

Expand All @@ -167,11 +158,12 @@ protected function openFile($mode)
$res = $this->fileAccessMode[$mode];
$file = new SplFileObject($path, $res['mode']);

if (self::FILE_READ == $mode) {
if ($mode === self::FILE_READ) {
$file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD);
}

if (!$this->getConfig()->useGzip() && !$file->flock($res['operation'])) {
$file = null;
throw new Exception('Could not lock file: ' . $path);
}

Expand All @@ -183,7 +175,7 @@ protected function openFile($mode)
*
* @return SplTempFileObject
*/
public function openTempFile()
public function openTempFile(): SplTempFileObject
{
return new SplTempFileObject($this->getConfig()->getSwapMemoryLimit());
}
Expand All @@ -210,7 +202,7 @@ protected function closeFile(SplFileObject &$file)
*
* @return \Generator
*/
public function readFromFile()
public function readFromFile(): \Generator
{
$file = $this->openFile(static::FILE_READ);

Expand All @@ -228,7 +220,7 @@ public function readFromFile()
*
* @param string $line
*/
public function appendToFile($line)
public function appendToFile(string $line)
{
$file = $this->openFile(static::FILE_APPEND);
$file->fwrite($line);
Expand Down
9 changes: 0 additions & 9 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the Flintstone package.
*
* (c) Jason M <emailfire@gmail.com>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Flintstone;

class Exception extends \Exception
Expand Down
Loading

0 comments on commit 4cd00b2

Please sign in to comment.