Skip to content

Instantly share code, notes, and snippets.

Avatar

Kerem Güneş k-gun

View GitHub Profile
View gist:43a1c26f49be17d44d8012c44ae94a3e
// Rewrite
- if ($shippingInfo && $shippingInfo['cost'] && $this->formatPrice($shippingInfo['cost'] != 0)) {
+ if (isset($shippingInfo['cost']) && $this->formatPrice($shippingInfo['cost']) > 0)
- $order_id = $this->request->get['order_id'];
+ $order_id = (int) $this->request->get['order_id'];
// Refactor
private function formatPrice($number)
{
View canvasing.html
<canvas id="c" width="1024" height="1024">
<script>
// Creds: https://twitter.com/aemkei/status/1378106731386040322
// let cx = c.getContext('2d'), x, y
// for (x = 0; x < 256; x++) {
// for (y = 0; y < 256; y++) {
// if ((x ^ y) % 2) {
// cx.fillRect(x * x, y * y, x, y);
// }
// }
@k-gun
k-gun / intdiv.php
Last active Mar 15, 2021
intdiv samples & polyfill.
View intdiv.php
<?php
// https://github.com/itchyny/gojq#difference-to-jq
$intdiv = fn($x, $y) => ($x - $x % $y) / $y;
// polyfill
if (!function_exists('intdiv')) {
function intdiv($x, $y) {
return ($x - $x % $y) / $y;
}
}
View birthday_paradox.js
// https://betterexplained.com/examples/birthday/birthday.html
function calc(people) {
let ret = {
days: 365,
people: people
}
ret.combinations = ret.people * (ret.people - 1) / 2
ret.chance = (ret.days - 1) / ret.days
ret.expected = 1 - (ret.chance ** ret.combinations)
@k-gun
k-gun / each.php
Last active Jun 2, 2021
Polyfill for "each" function that was removed as of PHP/8
View each.php
<?php
if (!function_exists('each')) {
function each(array &$array) {
$value = current($array);
$key = key($array);
if (is_null($key)) {
return false;
}
View convert_base.js
function convert_base(input, fromBase, toBase)
{
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
if (typeof fromBase == 'number') {
if (fromBase < 2 || fromBase > 62) {
throw ('Invalid base for from chars, min=2 & max=62')
}
fromBase = chars.substr(0, fromBase)
@k-gun
k-gun / Uninitialized typed properties 4.bug
Created Dec 8, 2019
Another "uninitialized" thing..
View Uninitialized typed properties 4.bug
class A { protected int $x; }
class AA extends A { protected $x; }
// -> PHP Fatal error: Type of acme\AA::$ must be int (as in class acme\A)
No var name here "acme\AA::$".
@k-gun
k-gun / Uninitialized typed properties 3.bug
Last active Dec 4, 2019
Another "uninitialized" thing..
View Uninitialized typed properties 3.bug
$foo = new class() {
var ?array $a;
function dump() { var_dump($this->a); }
function sort() { ksort($this->a); }
};
// E1: Error: Typed property class@anonymous::$a must not be accessed before initialization
$foo->dump();
// E2: TypeError: ksort() expects parameter 1 to be array, null given (should throw E1 cos its an uninitialized property?)
@k-gun
k-gun / Uninitialized typed properties 2.bug
Created Nov 22, 2019
Weird behavior with uninitialized typed properties and __set/__get
View Uninitialized typed properties 2.bug
Link: https://bugs.php.net/bug.php?id=78859
---
Weird behavior with uninitialized typed properties and __set/__get
Seems calling a constructor is triggering __set magic for uninitialized typed properties. So it does not matter the property is public or private/protected.
I suppose the problem is __set/__get called before __construct when a type is given to a property. Also I if remove __get then I get object(acme\Options)#1 (1) { ["stack"]=> array(1) { ["stack"]=> array(1) { ["one"]=> int(1) } } }.
@k-gun
k-gun / Uninitialized typed properties.bug
Last active Nov 22, 2019
php bugreport that was gone spam
View Uninitialized typed properties.bug
Link: https://bugs.php.net/bug.php?id=78809
---
Uninitialized typed properties
While throwing when try to access to any uninitialized typed property (eg: $o->x == null),
the codes below are just causing notice.
BTW, error message is so weird. We were expecting something like this;