Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created October 29, 2012 13:54
Show Gist options
  • Select an option

  • Save weierophinney/3973688 to your computer and use it in GitHub Desktop.

Select an option

Save weierophinney/3973688 to your computer and use it in GitHub Desktop.
static helper
<?php
abstract class Helper
{
public static function createHash(Paste $paste, PasteService $service)
{
$seed = $paste->language . $paste->timestamp . $paste->content;
do {
$seed . = uniqid();
$hash = hash('sha1', $seed);
} while ($service->exists($hash));
return $hash;
}
}
class MongoPasteService implements PasteService
{
protected $collection;
public function __construct(MongoCollection $collection)
{
$this->collection = $collection;
}
public function create(Paste $paste)
{
$hash = Helper::createHash($paste, $this);
$paste->hash = $hash;
$this->collection->save((array) $paste);
return $paste;
}
public function exists($hash)
{
/* .. check if paste exists .. */
}
}
@mvriel
Copy link

mvriel commented Oct 30, 2012

I would have made the same, or at least a very similar, comment.

As I see it, functions introduce the same issues as static methods; they induce a hard and untestable coupling between two structural elements.
For example: if I were to write a unit test for the create method in your example I would be required to test whether the has is correctly generated and thus write testing code that checks the createHash() static method. The same thing would happen if createHash() would be a function and thus the same comments apply.

Please note that this comment even applies to Traits; these are untestable, deeply coupled bits of code.

With regards to LSP and functions; in my opinion LSP is only applicable to objects as you cannot substitute methods or functions (monkeypatching not included in this statement); just objects. To even be a bit bolder: LSP applies to object oriented programming, (userland) functions are to be considered procedural programming. As such these two are hard to unite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment