Skip to content

M1I03: Socket wrapper#58

Merged
s2x merged 2 commits into
mainfrom
feature/m1i03-socket-wrapper
May 1, 2026
Merged

M1I03: Socket wrapper#58
s2x merged 2 commits into
mainfrom
feature/m1i03-socket-wrapper

Conversation

@s2x
Copy link
Copy Markdown
Contributor

@s2x s2x commented May 1, 2026

Summary

Action File
Create src/Server/Socket/Connection.php
Create src/Server/Socket/Socket.php
Create tests/Server/Socket/SocketTest.php
  • Connection wraps a \Socket resource with read/write/close/peer name tracking
  • Socket wraps a listen \Socket resource with bind/listen/accept/close/setOption
  • Socket::accept() returns Connection, throws SocketAcceptException on failure
  • Socket::close() is idempotent, sets resource to null after close
  • Socket::setOption() uses 3 parameters matching PHP's socket_set_option
  • Exceptions throw SocketCreationException/SocketAcceptException when operations fail

Acceptance criteria

  • accept() returns Connection instance, throws on error
  • close() is idempotent, resource set to null
  • setOption() has 3 parameters: $level, $option, $value
  • SocketTest passes (bind, listen, close, constructor)

Closes #5

Copy link
Copy Markdown
Contributor Author

@s2x s2x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dzięki za dodanie wrappera socketów. Kilka uwag — szczególnie getPeerName() wymaga poprawki przed mergem.

Ogólne: niespójna obsługa błędów

Klasa Socket konsekwentnie rzuca wyjątki przy operacjach na zamkniętym sockecie. Connection jest niespójna: read()/write() zwracają false, setOption() i getPeerName() nie robią nic. Dobrze byłoby ujednolicić na rzucanie wyjątków — wtedy caller zawsze wie, czego się spodziewać.

Brak testów dla Connection

78 linii kodu w Connection.php nie ma żadnego dedykowanego testu poza assertInstanceOf() i close() użytych w testAcceptReturnsConnection. Brakuje pokrycia dla read(), write(), getPeerName(), setOption(), getLastActivity() i ścieżek "po zamknięciu".

Comment thread src/Server/Socket/Connection.php
Comment thread src/Server/Socket/Connection.php Outdated
Comment thread src/Server/Socket/Connection.php Outdated
Comment thread src/Server/Socket/Socket.php Outdated
Comment thread tests/Server/Socket/SocketTest.php
Comment thread tests/Server/Socket/SocketTest.php
Copy link
Copy Markdown
Contributor Author

@s2x s2x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes applied in 71e8fa2

All review comments have been addressed:

# Finding Fix
1 getPeerName() missing $closed guard + ignored return value Added both: throws RuntimeException on closed socket or failed lookup
2 lastActivity updated on failed read()/write() Now only updates on success
3 Connection::setOption() ignores return value Now checks === false and throws RuntimeException with error message
4 Socket::setOption() ignores return value Now checks return value; kept SocketCreationException to match existing exception hierarchy
5 testCloseIsIdempotent has no assertion Added reflection-based null assertion after second close
6 testAcceptReturnsConnection uses raw PHP functions Changed to use $socket->setOption(), $socket->bind(), $socket->listen() API

@s2x s2x mentioned this pull request May 1, 2026
@s2x s2x merged commit b57f617 into main May 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

M1I03: Socket wrapper

1 participant