Skip to content

Instantly share code, notes, and snippets.

View craig-davis's full-sized avatar
👋

Craig Davis craig-davis

👋
View GitHub Profile
@craig-davis
craig-davis / .gitconfig
Created August 27, 2021 15:47
Copy the latest commit hash to the clipboard on OSX
# Copy the latest hash to the clipboard
latest = log --pretty=format:'%H' -n1
h = !git latest && git latest | pbcopy
@craig-davis
craig-davis / assertion-with-message.php
Last active April 27, 2020 01:38
PHPUnit Assertion with Message
<?php
public function testAuthorHasPostsArray()
{
self::assertIsArray(
(new Author())->getPosts(),
'Author should have an accessible array of posts'
);
}
@craig-davis
craig-davis / assertion-without-message.php
Last active April 27, 2020 01:38
PHPUnit Assertion Without Message
<?php
public function testAuthorHasPostsArray()
{
$author = new Author();
self::assertIsArray($author->getPosts());
}
<?php
class Post
{
protected $sidebarTypes = [8, 11];
protected $authorEditTimeout = 2592000;
...
@@ -7,8 +7,14 @@ class Post
$isAuthor = $this->getAuthorId() == $user->getId();
+ $isSupervisor = $this->getRole() == 4;
+ $isSidebarPost = $this->getType() == 8;
$isTooOld = $this->created_at < time() — 2592000;
+ if ($isSupervisor && $isSidebarPost) {
+ return true;
+ }
+
<?php
class Post
{
public function canEdit(User $user) : bool
{
$isEditor = $user->getRole() == 3 && $this->getEditorId() == $user->getId();
$isAdmin = $user->getRole() > 5;
$isAuthor = $this->getAuthorId() == $user->getId();
$isSupervisor = $this->getRole() == 4;
<?php
class Post
{
public function canEdit(User $user) : bool
{
$isEditor = $user->getRole() == 3 && $this->getEditorId() == $user->getId();
$isAdmin = $user->getRole() > 5;
$isAuthor = $this->getAuthorId() == $user->getId();
$isTooOld = $this->created_at < time() — 2592000;
<?php
class Post
{
public function canEdit(User $user) : bool
{
$canEdit = false;
if (($user->getRole() == 3 && $this->getEditorId() == $user->getId()) || $user->getRole() > 5 || ($this->getAuthorId() == $user->getId() && $this->created_at < time() — 2592000)) {
$canEdit = true;
}
@craig-davis
craig-davis / complexity-inverted-control.php
Last active April 15, 2017 17:25
Code Without Else with an Inverted Control
<?php
function calculateSalePrice($item, $discount = 0) {
if ($discount > 0) {
return calculateDiscountedPrice($item->getPrice, $discount);
}
return $item->getPrice();
}
@craig-davis
craig-davis / complexity-without-else.php
Last active April 15, 2017 17:26
Code Without Else
<?php
function calculateSalePrice($item, $discount = 0) {
if ($discount == 0) {
return $item->getPrice();
}
return calculateDiscountedPrice($item->getPrice, $discount);
}