Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed May 12, 2024
1 parent 6127864 commit d3ac1ee
Show file tree
Hide file tree
Showing 39 changed files with 123 additions and 110 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started/app/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ If you run `php cli.php` in your terminal it will print `Hello world`.

You should already be familiar with the `$os` variable by now, if not go the [dedicated chapter](../operating-system/index.md).

The `$env` variable is the abstraction to deal with everything inputed in your script and everything output that comes out. It behaves like an immutable object, meaning you **must** always use the new instance returned by its methods.
The `$env` variable is the abstraction to deal with everything inputed in your script and every output that comes out. It behaves like an immutable object, meaning you **must** always use the new instance returned by its methods.

To change the returned exit code you can do `return $env->exit(1)`.

Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/app/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ new class extends Main {
This example will send back the image at `images/some-image.png`. If the image is not found then it will throw an exception.

??? note
The `main` function will catch all thrown exceptions and will return an empty `500` response. This is done to make sure not stack trace is ever shown to a user.
The `main` function will catch all thrown exceptions and will return an empty `500` response. This is done to make sure no stack trace is ever shown to a user.

During development if you want to see the exception you can catch all exceptions yourself and use `filp/whoops` to render it. Or you can use [Innmind's framework](../framework/index.md) and its [profiler](../framework/profiler.md).

Expand Down
8 changes: 4 additions & 4 deletions docs/getting-started/concurrency/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ composer require innmind/mantle:~2.1

Mantle works a bit like a reduce operation. The _reducer_ function allows to launch `Task`s and react to their results. Both the _reducer_ and tasks are run asynchronously.

=== "Reducer"
=== "Script"
```php
use Innmind\Mantle\Forerunner;
use Innmind\Http\Response;
Expand All @@ -29,7 +29,7 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau
=== "Carried value"
Like in a real reduce operation you need a carried value that will be passed to the reducer every time it's called.

Here we use a `Carried` class but you can use any type you wish.
Here we use a `Carried` class but you can use any type you want.

```php
use Innmind\Http\Response;
Expand Down Expand Up @@ -183,7 +183,7 @@ Mantle works a bit like a reduce operation. The _reducer_ function allows to lau

## Advantages

The first big advantage of this design is that your task is completely unaware that it is run asynchronously. It all depends on the `$os` variable inject (1). This means that you can easily experiment a piece of your program in an async context by what code calls it, your program logic itself doesn't have to change!
The first big advantage of this design is that your task is completely unaware that it is run asynchronously. It all depends on the `$os` variable injected (1). This means that you can easily experiment a piece of your program in an async context by what code calls it, your program logic itself doesn't have to change!
{.annotate}

1. If it comes from Mantle it's async otherwise it's sync.
Expand All @@ -202,4 +202,4 @@ The other advantage is that since all state is local you can compose async code
Most of these limitations are planned to be fixed in the future.

!!! warning ""
You may not want to use this in production just yet, or at least for mission critical code.
You may not want to use this in production just yet, or at least not for mission critical code.
4 changes: 2 additions & 2 deletions docs/getting-started/concurrency/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

Concurrency is about executing multiple parts of a program in way to waste as little time as possible. There is 2 ways possible to achieve this:

- asynchronity
- asynchronicity
- parallelism

Asynchronous code means there are at all times only one part of a program that's executed. But each part of the program advances ones after the other in the same process. This mode is useful when your program is I/O bound, for example if a part of your program makes an HTTP call then another part can be executed while you wait for the response. However if your program is CPU bound then this mode has no usefulness.
Asynchronous code means there are at all times only one part of a program that's executed. But each part of the program advances one after the other in the same process. This mode is useful when your program is I/O bound, for example if a part of your program makes an HTTP call then another part can be executed while you wait for the response. However if your program is CPU bound then this mode has no usefulness.

Parallel code means that multiple processes will be executed at the same time. Each process will be spread across the cores available on your CPU. This mode is useful when your program is CPU bound. But it comes with the disadvantage of coordinating the results of each process (when needed).

Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/concurrency/ipc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

When your program runs across multiple processes you may want to communicate between them to update some state.

Innmind IPC use unis sockets to send messages between processes.
Innmind IPC use unix sockets to send messages between processes.

## Installation

Expand Down
7 changes: 4 additions & 3 deletions docs/getting-started/concurrency/queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ $client

1. For now don't worry about this `null`, just know that it's required.

The client will execute anything only when the `run` method is run. In this case, because we reuse the client from aboce, it will create the exchange, the queue and bind them together and then publish one message that will end up in the queue.
The client will execute anything only when the `run` method is called. In this case, because we reuse the client from above, it will create the exchange, the queue and bind them together and then publish one message that will end up in the queue.

If everything works fine then it will return an [`Either`](../handling-data/either.md) with `null` on the right side. If any error occurs it will be a `Failure` on the left side.

Using a client that always declare the the exchange and queues that it requires allows for a hot declaration of your infrastructure when you try to use the client. And if the exchanges, queues and bindings already exist it will silently continue to execute as the structure is the way you expect on the AMQP server.
??? info
Using a client that always declare the the exchange and queues that it requires allows for a hot declaration of your infrastructure when you try to use the client. And if the exchanges, queues and bindings already exist it will silently continue to execute as the structure is the way you expect on the AMQP server.

Then to consume the queue:

Expand Down Expand Up @@ -113,7 +114,7 @@ Here we reuse the client from the first example to make sure we indeed have a `p

At this point the `run` method will return `42` on the right side of the `Either` or a failure on the left side.

In this case the carried value is an `int` but you can use any type you wish.
In this case the carried value is an `int` but you can use any type you want.

??? tip
If you only need to pull one message from the queue you should use `Innmind\AMQP\Command\Get` instead of `Consume`.
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started/framework/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ You can now do `php bin/console greet John` to print `Hello John`.
```

- `$container` is a service locator
- `$os` you've seen it in the previous chapter
- `$os` you've seen it in a previous chapter
- `$env` contains the environment variables

You can add as many commands as uou wish by chaining calls to the `command` method.
You can add as many commands as you wish by chaining calls to the `command` method.

## Composition

Expand Down
8 changes: 4 additions & 4 deletions docs/getting-started/framework/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ new class extends Http {
};
```

You can expose this server via `cd public/ && php -S localhost:8080`. If you run `curl http://localhost:8080/` it will return a `404` response which is the default behaviour when you dn'ont specify any route.
You can expose this server via `cd public/ && php -S localhost:8080`. If you run `curl http://localhost:8080/` it will return a `404` response which is the default behaviour when you didn't specify any route.

You can define a route like this:

Expand Down Expand Up @@ -52,7 +52,7 @@ new class extends Http {

Now `curl http://localhost:8080/` will return a `200` response with the content `Hello world`.

You can specify placeholder in your routes like this:
You can specify placeholders in your routes like this:

```php title="public/index.php" hl_lines="1 7 10 15"
use Innmind\Router\Route\Variables;
Expand Down Expand Up @@ -106,7 +106,7 @@ new class extends Http {
- `$request` is the parsed request sent by a user
- `$variables` gathers all the values described by the route template
- `$container` is a service locator
- `$os` you've seen it in the previous chapter
- `$os` you've seen it in a previous chapter
- `$env` contains the environment variables

## Composition
Expand All @@ -121,7 +121,7 @@ new class extends Http {
{
return $app
->mapRequestHandler(
static fn(RequestHandle $route) => new class($route) implements RequestHandler {
static fn(RequestHandler $route) => new class($route) implements RequestHandler {
public function __construct(
private RequestHandler $inner,
) {}
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/framework/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ composer require innmind/framework:~2.2

## Concepts

The framework is defined by an entrypoint defines the context in which the framework will be run. Each entrypoint exposes a `configure` method to configure an immutable `Application` object.
The framework is defined by an entrypoint that specify the context in which the framework will be run. Each entrypoint exposes a `configure` method to configure an immutable `Application` object.

`Application` is the way to describe what your program can do. This is the same class no matter which entrypoint you choose. This allows you to switch the execution context without modifying any line of your code (1).
{.annotate}
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/framework/middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ new class extends Http {
??? info
The notation `$app->map($middleware)` is just an invertion of who calls who for better chaining methods. If you look at the implementation it does `$middleware($app)`.

Since the middleware is plain old PHP object, you can also add parameters to it.
Since the middleware is a plain old PHP object, you can also add parameters to it.

Let's say your program is a website that is accessible both in french and english. Instead of adding a parameter in every route and pass it around in every layer of your program you could do:

Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/handling-data/either.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Here we use a `string` to represent an email, instead [we should use an object](
```

!!! success ""
Both approaches seem very similar but there's a big advantage to `Either`: a static analysis tool understand the flow of errors and can tell you if when calling `match` you don't handle all possible error values. No tool can help you do the same with exceptions.
Both approaches seem very similar but there's a big advantage to `Either`: a static analysis tool understands the flow of errors and can tell you if when calling `match` you don't handle all possible error values. No tool can help you do the same with exceptions.

Just like `Maybe` you can recover in case of an error via the `otherwise` method. For example in the case the email is already used, instead of failing we can decide to update the stored user.

Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started/handling-data/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Handling data is an essential part of any program. Handling them simply and in a

In this chapter you'll find the 3 most used data structures throughout Innmind.

You'll learn how to them for simple cases and how they become indispensable as a program grow.
You'll learn how to them for simple cases and how they become indispensable as a program grows.

??? note
Head to the [package documentation](https://github.com/Innmind/Immutable/blob/develop/docs/README.md) to learn about the other data structures.
Head to the [package documentation](https://github.com/Innmind/Immutable/) to learn about the other data structures.

## Installation

Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started/handling-data/maybe.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,6 @@ So far we _do nothing_ in case our user doesn't have a brother. But what if we w
{.annotate}

1. Such as the absence of a [file on the filesystem](../operating-system/filesystem.md) or the absence of an [entity from a storage](../orm/index.md).
2. Such as failing to [upload a file to an S3 bucket](https://github.com/Innmind/S3)
2. Such as failing to [upload a file to an S3 bucket](https://github.com/Innmind/S3).

It also as a [deferred mode like `Sequence`](sequence.md#deferred) that allows to not directly load in memory a value when you call `$sequence->get($index)`. The returned `Maybe` in this case will load the value when you call the `match` method.
It also has a [deferred mode like `Sequence`](sequence.md#deferred) that allows to not directly load in memory a value when you call `$sequence->get($index)`. The returned `Maybe` in this case will load the value when you call the `match` method.
18 changes: 9 additions & 9 deletions docs/getting-started/handling-data/sequence.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Since you'll not always have all the values known when creating a `Sequence`, yo
??? tip
You may also come across the notation `#!php $values = Sequence::of('foo')('bar')('baz')` in the ecosystem. This is a more _math like_ notation to look like a matrix augmentation.

You check the implementation of `Sequence::add()` you'll see that it is a shortcut to the `__invoke` method that allows this notation.
You check the implementation of `Sequence::add()` you'll see that it is an alias to the `__invoke` method that allows this notation.

If instead of adding a single value to the list you need to add multiple ones you would do:

Expand Down Expand Up @@ -289,7 +289,7 @@ For example let's you have a list of cities and you only want to keep the french
```

??? tip
And of instead you want all the cities outside of France you can replace `filter` by `exclude`.
And if instead you want all the cities outside of France you can replace `filter` by `exclude`.

The `filter` method is fine if you don't need the new `Sequence` type to change, here we go from `Sequence<string>` to `Sequence<string>`. But if you have a `Sequence<null|\SplFileObject>` and you want to remove the `null` values then `filter`, even though will do the job, will return a `Sequence<null|\SplFileObject>`. This is a limitation of [Psalm](../../philosophy/development.md#type-strictness).

Expand All @@ -307,7 +307,7 @@ $values; // Sequence<\SplFileObject>

### Pipeline

So far you've only seen how to do one action at a time. The simplicity of `Sequence` starts to shine when chaning multiple actions.
So far you've only seen how to do one action at a time. The simplicity of `Sequence` starts to shine when chaining multiple actions.

Let's try to retrieve all the visited cities for each username, keep the french ones and remove the country from the name.

Expand Down Expand Up @@ -397,7 +397,7 @@ Let's try to retrieve all the visited cities for each username, keep the french

With the declarative and imperative approach you have to deal with either a lot of indentation or a lot of variables. With a `Sequence` you just keep chaining methods.

Another nice upside to `Sequence` is when you try to build a pipeline and to see the different results if you switch some logic around. To achieve it you only need to move a method call up or down, while the other approached you need to be aware of conflicting variables.
Another nice upside to `Sequence` is when you try to build a pipeline and want to see the different results if you switch some logic around. To achieve it you only need to move a method call up or down, while the other approaches you need to be aware of conflicting variables.

## Extracting data

Expand Down Expand Up @@ -475,7 +475,7 @@ At some point you'll need to extract the values contained in a `Sequence` (1). S
$value2 === null; // returns true
```

??? Note
??? info
The imperative approach could be simplified via `#!php $values[$index] ?? null`, but then if the value at the index is itself `null` you can't differentiate if the index exists or not.

### And more
Expand All @@ -492,10 +492,10 @@ This is the mode you've seen so far. When calling `Sequence::of()` you specify a

### Deferred

Instead of specyfying the values you can use a `Generator` to populate the `Sequence`. Once a value is loaded it's kept in memory. The advantage is that you can loop over the same geenrator multiple times (1).
Instead of specyfying the values you can use a `Generator` to populate the `Sequence`. Once a value is loaded it's kept in memory. The advantage is that you can loop over the same generator multiple times (1).
{.annotate}

1. Using a `Generator` directly requires to call again the function that created it. But this means you may not end up with the same values (especially if geenrating objects).
1. Using a `Generator` directly requires to call again the function that created it. But this means you may not end up with the same values (especially if generating objects).

```php
$values = Sequence::of(1, 2, 3, 4);
Expand All @@ -515,7 +515,7 @@ The `Sequence` is then used exactly the same way as an in memory one.

### Lazy

With this mode you build a `Sequence` by passing a function that returns a `Generator`. This generator will be called each time you try to extract some data from the `Sequence`.
With this mode you build a `Sequence` by passing a function that returns a `Generator`. This function will be called each time you try to extract some data from the `Sequence`.

```php
$values = Sequence::lazy(static function() {
Expand All @@ -536,7 +536,7 @@ The `Sequence` is then used exactly the same way as an in memory one.
1. Such as reading a multi gigabyte file or reading from a socket.

??? info
This is where lies the root of the power of Innmind. Being able to work with infinte volumes of data as it were in memory.
This is where lies the root of the power of Innmind. Being able to work with infinte volumes of data as if it were in memory.

### Tips

Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/operating-system/clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The `at` method returns the `Maybe<PointInTime>` type to make sure you always ha

We need to calculate elapsed time, among other cases, when handling heartbeats when dealing with sockets or in tests to make sure some code is executed in a certain amount of time.

The usual approach is to use a call to `microtime()` at the start and an another at the end and subtract them. The problem with this approach is that you can end up with a negative duration. This happens when your machine re-synchronise its clock via the [NTP protocol](https://en.wikipedia.org/wiki/Network_Time_Protocol) and sometimes it can go back in time (1).
The usual approach is to use a call to `microtime()` at the start and an another at the end and subtract them. The problem with this approach is that you can end up with a negative durations. This happens when your machine re-synchronise its clock via the [NTP protocol](https://en.wikipedia.org/wiki/Network_Time_Protocol) and sometimes it can go back in time (1).
{.annotate}

1. To avoid this problem the solution is to use a monotonic clock (via the `hrtime()` PHP function).
Expand Down
Loading

0 comments on commit d3ac1ee

Please sign in to comment.