Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Class and Helper Functions #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
33 changes: 33 additions & 0 deletions base58.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you can add use StephenHill\StaticBase58 to access class name StaticBase58 without namespace.


if (false === function_exists('base58_encode'))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need in false ===, it looks weird. I think

if (!function_exists('base58_encode'))

Is 100% enough and much more readable :)

{
function base58_encode($string)
{
return StephenHill\StaticBase58::encode($string);
}
}

if (false === function_exists('base58_decode'))
{
function base58_decode($string)
{
return StephenHill\StaticBase58::decode($string);
}
}

if (false === class_exists('Base58', true))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. Just use

if (!class_exists('Base58', true))

{
class Base58
{
public static function encode($string)
{
return StephenHill\StaticBase58::encode($string);
}

public static function decode($string)
{
return StephenHill\StaticBase58::decode($string);
}
}
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"psr-4": {
"StephenHill\\": "src/",
"StephenHill\\Benchmarks\\": "benchmarks/"
}
},
"files": [
"base58.php"
]
}
}
28 changes: 28 additions & 0 deletions src/StaticBase58.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace StephenHill;

class StaticBase58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is singleton, then you should add private function __construct() {}.

{
protected static $instance;

public static function encode($string)
{
return self::getInstance()->encode($string);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return static::... is better if somebody decide to override getInstance() to use another Base58 implementation class`

}

public static function decode($string)
{
return self::getInstance()->decode($string);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return static::... is better if somebody decide to override getInstance() to use another Base58 implementation class`

}

protected static function getInstance()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to make it public, so users can still configure this global instance. Actually, it's very strange that Base58 class does not have methods like setAlphabet() and setMathService(). I think it's not very good practice to implement everything in __construct().

{
if (null === static::$instance)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!static::$instance) is enough

{
static::$instance = new Base58();
}

return static::$instance;
}
}
27 changes: 27 additions & 0 deletions tests/StaticTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use StephenHill\StaticBase58;

class StaticTests extends PHPUnit_Framework_TestCase
{
public function testStaticClass()
{
$this->assertSame('JxF12TrwUP45BMd', StaticBase58::encode('Hello World'));
$this->assertSame('Hello World', StaticBase58::decode('JxF12TrwUP45BMd'));
}

public function testNamespacelessClass()
{
$this->assertTrue(class_exists('Base58', true));
$this->assertSame('JxF12TrwUP45BMd', Base58::encode('Hello World'));
$this->assertSame('Hello World', Base58::decode('JxF12TrwUP45BMd'));
}

public function testFunctions()
{
$this->assertTrue(function_exists('base58_encode'));
$this->assertTrue(function_exists('base58_decode'));
$this->assertSame('JxF12TrwUP45BMd', base58_encode('Hello World'));
$this->assertSame('Hello World', base58_decode('JxF12TrwUP45BMd'));
}
}