From 535c14668d1305dac3e282e6cd01acf40586cb91 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 5 Apr 2025 16:01:49 -0400 Subject: [PATCH] stan --- .gitignore | 2 ++ Books.php | 26 --------------- LazyObject.php | 12 ------- NullCoalescingAssignment.php | 3 -- composer.json | 5 +++ ex/Example2.php | 6 ---- ex/MyClass.php | 9 ----- huge.txt | 4 +-- phpstan.neon | 5 +++ BcMath.php => src/BcMath.php | 0 src/Books.php | 32 ++++++++++++++++++ Coffee.php => src/Coffee.php | 0 Dog.php => src/Dog.php | 0 LazyCollection.php => src/LazyCollection.php | 1 + LazyGhost.php => src/LazyGhost.php | 2 +- src/LazyObject.php | 19 +++++++++++ src/NullCoalescingAssignment.php | 8 +++++ src/ex/Example2.php | 6 ++++ src/ex/MyClass.php | 9 +++++ src/generics.php | 11 ++++++ src/huge.txt | 3 ++ src/libs/Collection.php | 23 +++++++++++++ {libs => src/libs}/Dollars.php | 3 +- {libs => src/libs}/LazyCollection.php | 35 ++++++++++++++++++-- {libs => src/libs}/LazyObject.php | 2 +- src/libs/Scalar.php | 13 ++++++++ {libs => src/libs}/autoload.php | 0 27 files changed, 174 insertions(+), 65 deletions(-) create mode 100644 .gitignore delete mode 100644 Books.php delete mode 100644 LazyObject.php delete mode 100644 NullCoalescingAssignment.php create mode 100644 composer.json delete mode 100644 ex/Example2.php delete mode 100644 ex/MyClass.php mode change 100644 => 120000 huge.txt create mode 100644 phpstan.neon rename BcMath.php => src/BcMath.php (100%) create mode 100644 src/Books.php rename Coffee.php => src/Coffee.php (100%) rename Dog.php => src/Dog.php (100%) rename LazyCollection.php => src/LazyCollection.php (91%) rename LazyGhost.php => src/LazyGhost.php (91%) create mode 100644 src/LazyObject.php create mode 100644 src/NullCoalescingAssignment.php create mode 100644 src/ex/Example2.php create mode 100644 src/ex/MyClass.php create mode 100644 src/generics.php create mode 100644 src/huge.txt create mode 100644 src/libs/Collection.php rename {libs => src/libs}/Dollars.php (88%) rename {libs => src/libs}/LazyCollection.php (59%) rename {libs => src/libs}/LazyObject.php (92%) create mode 100644 src/libs/Scalar.php rename {libs => src/libs}/autoload.php (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8a7996 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +composer.lock +vendor/ diff --git a/Books.php b/Books.php deleted file mode 100644 index bd10338..0000000 --- a/Books.php +++ /dev/null @@ -1,26 +0,0 @@ - "Authors: David Smith\n"; - } - - public Author $mainAuthor { - get => new Author("Smith"); - set => throw new Exception("Main author cannot be changed."); - } -} - -$book = new Book(); -echo $book->credits; - -$author = $book->mainAuthor; -echo $author->name; diff --git a/LazyObject.php b/LazyObject.php deleted file mode 100644 index e23c841..0000000 --- a/LazyObject.php +++ /dev/null @@ -1,12 +0,0 @@ -proxy(301); -// The object is initialized only when accessed -echo $objectA->someMethod() . "\n"; - -$lazyB = new libs\LazyObject(ex\Example2::class); -$objectB = $lazyB->proxy(); -$objectB->hi(); diff --git a/NullCoalescingAssignment.php b/NullCoalescingAssignment.php deleted file mode 100644 index 0f24057..0000000 --- a/NullCoalescingAssignment.php +++ /dev/null @@ -1,3 +0,0 @@ -num; - } -} diff --git a/huge.txt b/huge.txt deleted file mode 100644 index 25d7951..0000000 --- a/huge.txt +++ /dev/null @@ -1,3 +0,0 @@ -Hello, -you@ -guys! diff --git a/huge.txt b/huge.txt new file mode 120000 index 0000000..626bf5e --- /dev/null +++ b/huge.txt @@ -0,0 +1 @@ +src/huge.txt \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..a3d84d9 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: max + + paths: + - src diff --git a/BcMath.php b/src/BcMath.php similarity index 100% rename from BcMath.php rename to src/BcMath.php diff --git a/src/Books.php b/src/Books.php new file mode 100644 index 0000000..3b120c8 --- /dev/null +++ b/src/Books.php @@ -0,0 +1,32 @@ +_mainAuthor = new Author("Smith"); // Initialize + } + + public string $credits { + get => "Authors: David Smith\n"; + } + + public Author $mainAuthor { + get => $this->_mainAuthor; // Now reads the stored value + set => throw new \Exception("Main author cannot be changed."); + } +} + +$book = new Book(); +echo $book->credits; // "Authors: David Smith\n" + +$author = $book->mainAuthor; +echo $author->name; // "Smith" diff --git a/Coffee.php b/src/Coffee.php similarity index 100% rename from Coffee.php rename to src/Coffee.php diff --git a/Dog.php b/src/Dog.php similarity index 100% rename from Dog.php rename to src/Dog.php diff --git a/LazyCollection.php b/src/LazyCollection.php similarity index 91% rename from LazyCollection.php rename to src/LazyCollection.php index 1a35fbe..28fd14d 100644 --- a/LazyCollection.php +++ b/src/LazyCollection.php @@ -19,6 +19,7 @@ $lazyCollection = libs\LazyCollection::make(function () { ob_start(); $lazyCollection->each(function ($line) { + $line = new libs\Scalar()->intoString($line); echo $line; ob_flush(); flush(); diff --git a/LazyGhost.php b/src/LazyGhost.php similarity index 91% rename from LazyGhost.php rename to src/LazyGhost.php index 15b02b0..ea36234 100644 --- a/LazyGhost.php +++ b/src/LazyGhost.php @@ -14,7 +14,7 @@ $reflector = new ReflectionClass(Example::class); $object = $reflector->newLazyGhost($initializer); var_dump($object); -var_dump($object instanceof Example); +// var_dump($object instanceof Example); // Triggers initialization, and fetches the property after that var_dump($object->data); diff --git a/src/LazyObject.php b/src/LazyObject.php new file mode 100644 index 0000000..be2860f --- /dev/null +++ b/src/LazyObject.php @@ -0,0 +1,19 @@ +proxy($i); + // The object is initialized only when accessed + $data = $objectA->someMethod(); + $data = new libs\Scalar()->intoString($data); + echo $data; +}); + +$lazyB = new libs\LazyObject(ex\Example2::class); +$objectB = $lazyB->proxy(); +$objectB->hi(); + +$runA(800); diff --git a/src/NullCoalescingAssignment.php b/src/NullCoalescingAssignment.php new file mode 100644 index 0000000..4740d63 --- /dev/null +++ b/src/NullCoalescingAssignment.php @@ -0,0 +1,8 @@ +intoString($name); + +echo $name; diff --git a/src/ex/Example2.php b/src/ex/Example2.php new file mode 100644 index 0000000..981836f --- /dev/null +++ b/src/ex/Example2.php @@ -0,0 +1,6 @@ +num; + } +} diff --git a/src/generics.php b/src/generics.php new file mode 100644 index 0000000..867c3bc --- /dev/null +++ b/src/generics.php @@ -0,0 +1,11 @@ +all(); + +foreach($names as $name) { + $name = new libs\Scalar()->intoString($name); + echo "Hello, " . $name . PHP_EOL; +} diff --git a/src/huge.txt b/src/huge.txt new file mode 100644 index 0000000..25d7951 --- /dev/null +++ b/src/huge.txt @@ -0,0 +1,3 @@ +Hello, +you@ +guys! diff --git a/src/libs/Collection.php b/src/libs/Collection.php new file mode 100644 index 0000000..5979d98 --- /dev/null +++ b/src/libs/Collection.php @@ -0,0 +1,23 @@ + $items + */ + public function __construct( + private array $items + ) { + } + + /** + * @return array + */ + public function all(): array { + return $this->items; + } +} diff --git a/libs/Dollars.php b/src/libs/Dollars.php similarity index 88% rename from libs/Dollars.php rename to src/libs/Dollars.php index 8d2cc81..297db71 100644 --- a/libs/Dollars.php +++ b/src/libs/Dollars.php @@ -14,10 +14,11 @@ class Dollars { return '$' . number_format($this->moneyAsFloat($money), 2, '.', ','); } - public function intlUSD(Number $money): string { + public function intlUSD(Number $money): string|false { if (class_exists('NumberFormatter')) { $formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); return $formatter->formatCurrency($this->moneyAsFloat($money), 'USD'); } + return false; } } diff --git a/libs/LazyCollection.php b/src/libs/LazyCollection.php similarity index 59% rename from libs/LazyCollection.php rename to src/libs/LazyCollection.php index cd6fd0e..1747fb7 100644 --- a/libs/LazyCollection.php +++ b/src/libs/LazyCollection.php @@ -1,11 +1,19 @@ + */ class LazyCollection implements \IteratorAggregate { + /** @var \Closure|self|array|null */ protected $source; + /** + * @param \Closure(): iterable|self|iterable|null $source + */ public function __construct($source = null) { if ($source instanceof \Closure || $source instanceof self) { @@ -17,11 +25,18 @@ class LazyCollection implements \IteratorAggregate } } + /** + * @return \Traversable + */ public function getIterator(): \Traversable { return $this->makeIterator($this->source); } + /** + * @param \Closure(): iterable|self|array $source + * @return \Traversable + */ protected function makeIterator($source) { if ($source instanceof \Closure) { @@ -35,12 +50,22 @@ class LazyCollection implements \IteratorAggregate return $source; } + /** + * @template TNewKey + * @template TNewValue + * @param \Closure(): iterable|iterable|null $source + * @return self + */ public static function make($source = null) { return new static($source instanceof \Closure ? $source() : $source); } - public function each(callable $callback) + /** + * @param callable(TValue, TKey): bool $callback + * @return self + */ + public function each(callable $callback): LazyCollection { foreach ($this->getIterator() as $key => $value) { if ($callback($value, $key) === false) { @@ -51,7 +76,11 @@ class LazyCollection implements \IteratorAggregate return $this; } - protected function getArrayableItems($items) + /** + * @param mixed $items + * @return array + */ + protected function getArrayableItems($items): array { if (is_array($items)) { return $items; diff --git a/libs/LazyObject.php b/src/libs/LazyObject.php similarity index 92% rename from libs/LazyObject.php rename to src/libs/LazyObject.php index 3296f98..8f99322 100644 --- a/libs/LazyObject.php +++ b/src/libs/LazyObject.php @@ -5,7 +5,7 @@ namespace libs; class LazyObject { public function __construct(public string $className) {} - public function proxy(mixed ...$args) { + public function proxy(mixed ...$args): Object { $className = $this->className; if (!class_exists($className, true)) { throw new \InvalidArgumentException("Class {$className} does not exist"); diff --git a/src/libs/Scalar.php b/src/libs/Scalar.php new file mode 100644 index 0000000..bd0b36b --- /dev/null +++ b/src/libs/Scalar.php @@ -0,0 +1,13 @@ +