Skip to content

Instantly share code, notes, and snippets.

Amo Chohan amochohan

Block or report user

Report or block amochohan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
amochohan / array_merge_sum.php
Created Jun 5, 2015
PHP merge two arrays and sum the total of a common indexes
View array_merge_sum.php
"USD" => 10000,
"GBP" => 20000
"USD" => 10000,
"GBP" => 20000,
"JPY" => 100
amochohan /
Created Oct 6, 2015
Convert an SQLite database to MySQL dump - fix malformed database error

Fix a malformed SQLite database

From time to time, when running integration tests you'll notice that tests that worked previously have stopped working. Trying to modify the schema of an SQLite database is throwing an error. The dreaded:

Error (11) malformed database

Fortunately, there's quite an easy fix. Simply head to the command line, and use the following commands:

sqlite3 yourdatabase.sqlite

amochohan / reverse-string.php
Created Sep 24, 2014
Recurrsive function to reverse a string
View reverse-string.php
function reverseString($string){
//Is this method being recurrsively called, i.e. do we still have part of the string being passed to the function to be reversed?
* Return the last letter of the string, and append via calling this method again recursively (passing the remainder of the un-reversed string)
* to continue reversing the remainder of the string
amochohan / .php_cs.dist.php
Created Feb 21, 2017
PHP code style rules
View .php_cs.dist.php
return PhpCsFixer\Config::create()
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_unsets' => true,
amochohan / example.php
Created Mar 6, 2017
Model factories - making relationships
View example.php
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
View ordering-eloquent-results.php
// Instead of using the following code
$builder = User::with('somerelationship');
if ($request->has('order_by')) {
$users = $builder->get();
amochohan / retry.php
Created Apr 11, 2017
Retrying a task
View retry.php
try($attempts = 1, $wait = 0) {
// Try $attempts times to do something, waiting $wait milliseconds between trys
// Within the try block scope, we have access to the $attempts and $wait arguments
$this->log('Doing something important attempt: ' . $attempt);
catch (Exception $e) {
// Do something
amochohan / pick.php
Created Apr 25, 2017
Performing a partial update
View pick.php
MyController extends Controller
public function store(Request $request)
$interaction = Interaction::create($this->pick((new Interaction)->getFillable(), $request);
amochohan / example.php
Created Oct 27, 2017
Generating a timeseries - Carbon vs native PHP
View example.php
$timeSeries = [];
// Using Carbon
$start = \Carbon\Carbon::create(1983, 9, 1, 0, 0, 0);
$end = \Carbon\Carbon::create(2050, 1, 1, 0, 0,0);
while ($start->format('Y-m-d') < $end->format('Y-m-d')) {
View CalculatorTest.php
class CalculatorTest extends TestCase
/** @test */
public function it_adds_two_numbers()
$calculator = new Calculator();
$total = $calculator->sum(1, 2);
$this->assertSame(3, $total);
You can’t perform that action at this time.