Skip to content

Instantly share code, notes, and snippets.

@thepsion5
thepsion5 / SlowQueryLogDecorator.php
Created December 21, 2022 20:06
Using Aura\SQL to Log Slow Queries via a simple Decorator Class
<?php
declare(strict_types=1);
namespace YourApp\Infrastructure\Logging;
use Psr\Log\{LoggerInterface, LoggerTrait};
class SlowQueryLogDecorator implements LoggerInterface
{
use LoggerTrait;
@thepsion5
thepsion5 / RendersTemplates.php
Created February 1, 2022 17:50
Example of Using Composition via Traits to Remove Duplicate Controller Logic
<?php
declare(strict_types=1);
namespace Personly\Http\Traits;
use Personly\Framework\Rendering\TemplateRenderer;
use Symfony\Component\HttpFoundation\{JsonResponse, Response};
trait RendersTemplates
@thepsion5
thepsion5 / original.php
Last active December 29, 2021 19:06
Refactoring Procedural Code for a Product Filter
<?php
$SQL = "SELECT inv.*, COUNT(*) OVER() AS totalrows FROM stones inv WHERE wt >= 2.5";
if (isset($_POST["ItemSearch"])) $SQL .= "AND number LIKE '" . $_POST["ItemSearch"] . "%'";
if (isset($_POST["minimum_wt"], $_POST["maximum_wt"]) && !empty($_POST["minimum_wt"]) && !empty($_POST["maximum_wt"])) $SQL .= "AND wt BETWEEN '" . $_POST["minimum_wt"] . "' AND '" . $_POST["maximum_wt"] . "'";
if (isset($_POST["shape"])) {
@thepsion5
thepsion5 / MinLengthValidationRule.php
Created November 23, 2021 20:43
Example of A Flexible Validation Solution Using Classes For Validation Rules
<?php
/**
* Validates a value based on its length as a string
*
* ```php
* $toBeValidated = 17;
* $validationParameters = [
* 'min' => 10
* ];
@thepsion5
thepsion5 / script.js
Created July 10, 2020 16:58
Tampermonkey Script to hide the "Trending Now" sidebar on Twitter
// ==UserScript==
// @name Trending Now No More
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Removes the Trending Now section on Twitter
// @author Sean Mumford
// @match https://twitter.com/*
// @grant none
// ==/UserScript==
@thepsion5
thepsion5 / bootstrap.php
Created January 13, 2020 15:51
Example of Autoloading In a Legacy PHP Application
<?php
//FILE LOCATION: project-directory/config/bootstrap.php
declare(strict_types=1);
//Composer autoloader file
require_once __DIR__ . '/../vendor/autoload.php';
error_reporting(E_ALL | E_NOTICE);
//Load the files required for configuration
$configuration = require __DIR__ . '/settings.php';
$countyList = require __DIR__ . '/../vendor/some-other-package/list-of-tennessee-counties.php';
@thepsion5
thepsion5 / example_query.php
Created December 19, 2019 19:11
Example solution for a question posted in a PHP Facebook group
<?php
/*
The question:
if ( $date_text == 'Sunday' ){
$sql = "update table set sun = $date where "id ='$_REQUEST[num]";
$sql1= "update table set sun = $date where "id ='$_REQUEST[num1]";
mysql_query($con,$sql);
mysql_query($con,$sql1);
}
@thepsion5
thepsion5 / User.php
Last active August 23, 2019 20:05
Example of a PHP class that stores dynamic properties in an array
<?php
/**
* Example of a PHP class that stores dynamic properties in an array, and then can be used to create a SQL statement
*
* I would consider this a proof-of-concept and I do not believe this is a good practice
*/
class User
{
/**
* @var array Object attributes
@thepsion5
thepsion5 / TwitterProfileTest.php
Created September 28, 2018 21:20
Example of a unit test for an implementation of a Twitter Profile PHP class taken from a project at work, using PHPUnit
<?php
namespace App\Tests\Domain\Twitter;
use App\Domain\Twitter\TwitterProfile;
class TwitterProfileTest extends \App\Tests\BaseUnitTest
{
private $accessToken = 'oauth-token';
private $accessSecret = 'oauth-secret';
@thepsion5
thepsion5 / pagination.php
Created September 3, 2018 18:02
A simple example of procedural PHP code that can be used to generated paginated results from a database query
<?php
require 'setup/connection.php'; //returns a PDO instance;
$searchTerm = isset($_GET['search']) ? $_GET['search'] : null;
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
//Step 1: Get the total number of results for a search
$sqlParams = [];
$totalResultSql = 'SELECT COUNT(*) AS count FROM articles ';
if ($searchTerm) {
$sqlParams['search'] = "%$searchTerm%";